Why doesn't jQuery triggerHandler () prevent inline events?

I did this test code for the question: http://jsfiddle.net/5phqm/1/

As far as I understand, if jQuery triggerHandler()prevents the default behavior of the browser, then the original JavaScript events will not be triggered and processed (and this is true for addEventListener()in my code), but the inline event added through the tag attribute onclick=""will be fired anyway! Why it happens? I don’t understand something about events triggered in the browser?

+5
source share
2 answers

You can confirm that inline handlers execute because it is explicitly encoded :

handle = ontype && cur[ ontype ];
if ( handle && jQuery.acceptData( cur ) && handle.apply( cur, data ) === false ) {
    event.preventDefault();
}

where ontypein this case "onclick". Thus, it retrieves the property of the onclickelement and then executes it. This piece of code is always invoked regardless of .trigger/ .triggerHandler.

Own actions, however, are elem.click()performed inside the blockif :

if ( !onlyHandlers && !event.isDefaultPrevented() ) {
    // ...
    elem[ type ]();

where onlyHandlers- truefor triggerHandleand falsefor .trigger, and therefore triggerHandlernot executed, for example. elem.click()(whereas .trigger). As such, native action is prevented.

Therefore, built-in handlers and own actions are separate things and are also processed separately.

.triggerHandler only native actions are prevented.
+2

( , jQuery, ), jQuery , jQuery.trigger.event, -

   $(elem).data("events");

, . , .

0

All Articles