我这样做:
这不起作用。为什么?
我这么做:
$.each($('img'), function () {
this.unbind('onmouseover');
});
这不起作用。为什么?
我这么做:
$.each($('img'), function () {
this.unbind('onmouseover');
});
可以尝试以下方法:
$('img').unbind('mouseover');
不需要循环,而且应该是mouseover
而不是onmouseover
假设:您正在使用.bind
将mouseover
处理程序绑定
我没有使用bind。有些图像具有onmouseover属性,我想将它们删除。我尝试过$('img').removeAttr('onmouseover'),但仍然不起作用。
代码:
$('img').on('mouseover', function () {
//Your code
});
之后可以使用.off
来取消绑定 ->
$('img').off('mouseover');
一个解决你当前问题的方法(不太推荐),(参考)
$.each($('img'), function () {
$(this).removeAttr('onmouseover');
});
此外,在jQuery中,您可以“串联”移除处理程序方法,因为每个函数都返回相同的集合。每个附加方法都有自己的删除方法对,因此请相应地使用。
最后,要删除DOM元素上的处理程序(内联事件处理程序),请将其替换为null或执行return false
的函数。
这是概念代码:
$('img')
.unbind('mouseover') //remove events attached with bind
.off('mouseover') //remove events attached with on
.die('mouseover'); //remove events attached with live
.each(function(i,el){ //and for each element
el.onmouseover = null //replace the onmouseover event
});
.onmouseover
属性而不是null
是否有原因? - user1106925function(){return false}
是我知道的方法。 - JosephremoveAttr()
也可以移除处理程序。现在我学到了新东西 :) - Joseph
this
是 DOM 元素而不是 jQuery 对象,因此没有unbind
方法。 - Yoshi