javascript - Please tell me the difference and pros and cons of trigger('click') and click() in jquery.
为情所困
为情所困 2017-06-26 10:58:52
0
3
887
$('#btn').trigger('click');
$('#btn').click();

Which one of these two click event triggering methods is better?

为情所困
为情所困

reply all(3)
phpcn_u1582

jquery2 source code

jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
    "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
    "change select submit keydown keypress keyup error contextmenu").split(" "), function( i, name ) {

    // Handle event binding
    jQuery.fn[ name ] = function( data, fn ) {
        return arguments.length > 0 ?
            this.on( name, null, data, fn ) :
            this.trigger( name );
    };
});

It seems that the click execution event implementation of the $ object also calls trigger... so trigger is better? = =||Different ideas from @MockingBird- -

女神的闺蜜爱上我

trigger() is mainly used to trigger custom events

$( "#foo" ).on( "custom", function( event, param1, param2 ) {
  alert( param1 + "\n" + param2 );
});
$( "#foo").trigger( "custom", [ "Custom", "Event" ] );

@Dont posted the jquery source code. In fact, .click() also directly calls the .trigger() method, so the performance should be the same.

曾经蜡笔没有小新

Like jQuery.post() or jQuery.ajax(), which one is better? ?

You can obviously guess that the post must call ajax, so use ajax? Just because there is one less function call? Then type more words? waste time?

There is nothing good or bad. You have to think about other aspects. Being concise and easy to understand is also important.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!