Javascript jQuery 中的 addEventListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7025437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
addEventListener in jQuery
提问by Diana
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
同样来自一个非常好的 jQuery 教程:http: //itunes.apple.com/in/app/designmobileweb/id486198804?mt=8
What is the jQuery equivalent for the following statement;
以下语句的 jQuery 等价物是什么?
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)
如果是bind()方法,有没有指定最后一个参数的选项(即事件冒泡或捕获...true/false)
Thank you.
谢谢你。
回答by ShankarSangoli
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);
回答by Jake
Yes I'm pretty certain .bind() will work as you need it. Check out the jQuery .bind() docs pageI'm sure you can figure out the setup. Demo code below:
是的,我很确定 .bind() 会按您的需要工作。查看jQuery .bind() 文档页面,我相信您可以弄清楚设置。演示代码如下:
$(document).ready(function() {
$("#element1").bind('click', function() {
// do something on click
}
});
回答by Baz1nga
use jquerys bind method
使用 jquerys绑定方法
example:
例子:
document.bind("custom-listener", someCustomFunction, false);
document.trigger("custom-listener", {jsonArgsKey:jsonValue});
function someCustomFunction(json)
{
alert(json.jsonArgsKey);
}
回答by Abraham
Like this:
像这样:
$(element1).click(doSomething)
If you want to stop bubbling, call event.stopPropagation()
in your doSomething
function, like this:
如果您想停止冒泡,请调用event.stopPropagation()
您的doSomething
函数,如下所示:
function doSomething (event){
event.stopPropagation()
// do whatever
}
There's no way to set up a capturing event handler with jQuery, though.
但是,无法使用 jQuery 设置捕获事件处理程序。