84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
$('#btn').trigger('click');
$('#btn').click();
Which one of these two click event triggering methods is better?
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
trigger()
$( "#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.
.click()
.trigger()
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.
jquery2 source code
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@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.