addEventListener in jQuery [duplicate]

≯℡__Kan透↙ 提交于 2019-12-23 07:47:53

问题


Possible Duplicate:
jQuery equivalent of JavaScript's addEventListener method

Also from a very good jQuery tutorial on : http://itunes.apple.com/in/app/designmobileweb/id486198804?mt=8

What is the jQuery equivalent for the following statement;

element1.addEventListener('click',doSomething2,false)

If it is the bind() method, is there any option to specify the last parameter (i.e. event bubbling or capturing ...true/false)

Thank you.


回答1:


Try this

// Setting the third argument to false will attach a function
// that prevents the default action from occurring and 
// stops the event from bubbling.
$("#element1").bind("click", doSomething2, false);



回答2:


Yes I'm pretty certain .bind() will work as you need it. Check out the jQuery .bind() docs page I'm sure you can figure out the setup. Demo code below:

$(document).ready(function() {
    $("#element1").bind('click', function() {
        // do something on click
    } 
});



回答3:


use jquerys bind method

example:

document.bind("custom-listener", someCustomFunction, false);

document.trigger("custom-listener", {jsonArgsKey:jsonValue});

function someCustomFunction(json)
{
alert(json.jsonArgsKey);
}



回答4:


Like this:

$(element1).click(doSomething)

If you want to stop bubbling, call event.stopPropagation() in your doSomething function, like this:

function doSomething (event){
  event.stopPropagation()
  // do whatever
}

There's no way to set up a capturing event handler with jQuery, though.



来源:https://stackoverflow.com/questions/7025437/addeventlistener-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!