javascript - pass event to eventhandler -


I sometimes get confused with how to conduct incidents. I just wonder how to make this event happen to his handler.

The following example works just fine:

  $ (document) .on ('click' "Div # foo", function (event) {event.preventDefault ();});   

How to present an assigned handler with the name, such as:

  // This does not clearly work $ $ ('div # foo ') .on (' click ', fooClick); Ceremony fooClick (event) {event.preventDefault (); }   

Thank you for your insights!

Your second example is exactly how to do it and it will work just fine.

This is the responsibility of the call .on () to create a logic to make a callback call, and do not do anything with that, how to do your callback The first argument for the callback function declares that the event object does not matter how the callback is declared.

So, it will work just fine:

  function fooClick (event) {event.preventDefault (); } // This works only on fine $ ('div # foo') ('Click', fooClick);   

Work demo:

What is the key to understanding when you pass fooClick as the second argument on .on () , you're passing the function reference bus. It is .on () which decides to call the reference of that function and pass it.


FYI, your selectors will usually perform better if you only #foo , not div # foo unless you specifically Do not want to match only #foo , if it's in a div tag, since the id value can only be used once in the given page, They do not need to qualify in the form and by doing this, the need is more unnecessary for the engineer. In comes.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -