$('#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.