Why does a jQuery ("remove") trigger do the same as remove () without a trigger?

It looks like the following code:

$("#logo-events").trigger("remove");

Same as

$("#logo-events").remove();

Is this expected behavior? You can try it on jquery by opening the console.

+4
source share
3 answers

From the method documentation .trigger():

For ordinary objects and DOM objects other than a window, if the called event name matches the property name of the object, jQuery will try to call the property as a method , if there is no event, the handler calls event.preventDefault (). If this behavior is undesirable, use .triggerHandler () instead

.remove() DOM:

remove() :
- , .
- .

, . "remove", jQuery .remove() node, .

jQuery/event.js:

if ( ontype && jQuery.isFunction( elem[ type ] ) && !jQuery.isWindow( elem ) ) {
    // ...
    elem[ type ]();    // elem["remove"]();
    // ...
}
+5

.trigger() - , .remove() - , ( ). .trigger('remove'), , , .

-1

, , . ( ) , . "on()".

( "#logo-events" ).on( "click", function() { alert( $( this ).text() ); }); $( "#logo-events" ).trigger( "click" );

, , , , , .

:

$("logo-events").trigger("remove");

.

, .

-1

Source: https://habr.com/ru/post/1605967/


All Articles