jQuery中的addEventListener
可能重复:
jQuery相当于JavaScript的addEventListener方法
还有一个非常好的jQuery教程: http : //itunes.apple.com/in/app/designmobileweb/id486198804?mt = 8
以下语句的jQuery等价物是什么;
element1.addEventListener('click',doSomething2,false)
如果是bind()方法,是否有任何选项来指定最后一个参数(即事件冒泡或捕获… true / false)
谢谢。
试试这个
// 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);
是的我很确定.bind()会在你需要的时候工作。 查看jQuery .bind()文档页面我相信你可以找出设置。 演示代码如下:
$(document).ready(function() { $("#element1").bind('click', function() { // do something on click } });
使用jquerys 绑定方法
例:
document.bind("custom-listener", someCustomFunction, false); document.trigger("custom-listener", {jsonArgsKey:jsonValue}); function someCustomFunction(json) { alert(json.jsonArgsKey); }
像这样:
$(element1).click(doSomething)
如果要停止冒泡,请在doSomething
函数中调用event.stopPropagation()
,如下所示:
function doSomething (event){ event.stopPropagation() // do whatever }
但是,没有办法用jQuery设置捕获事件处理程序。