Javascript 从所有系统中删除onmouseover事件<;img>;标签

Javascript 从所有系统中删除onmouseover事件<;img>;标签,javascript,jquery,html,asp.net-mvc-3,Javascript,Jquery,Html,Asp.net Mvc 3,我这样做: $.each($('img'), function () { this.unbind('onmouseover'); }); 这是行不通的。为什么?试试下面的方法 $('img').unbind('mouseover'); 不需要循环。。而且它应该是mouseover而不是onmouseover 假设:您正在使用.bind绑定鼠标上方的处理程序 我没有用bind。有些图像有onmouseover属性,我想删除它们。我尝试了$('img')。removeAttr('onmo

我这样做:

$.each($('img'), function () {
    this.unbind('onmouseover');
});
这是行不通的。为什么?

试试下面的方法

$('img').unbind('mouseover');
不需要循环。。而且它应该是
mouseover
而不是
onmouseover

假设:您正在使用
.bind
绑定
鼠标上方的
处理程序


我没有用bind。有些图像有onmouseover属性,我想删除它们。我尝试了$('img')。removeAttr('onmouseover'),但仍然不起作用

  • 使用内联事件处理程序不是标准
  • 因为您使用的是jQuery,所以应该像下面这样绑定处理程序
  • 代码:

    $('img').on('mouseover', function () {
         //Your code
    });
    
    以后可以使用
    .off
    ->

    $('img').off('mouseover');
    
    针对您所拥有的(非首选)的解决方案,()

    • 当jQuery元素集合已经具有内部

    • 此外,您还可以“菊花链”删除jQuery中的处理程序方法,因为每个函数都返回相同的集合。每个attach方法都有自己的remove方法对,因此请相应地使用

      • =>
      • =>
      • =>
    • 最后,要删除DOM元素上的处理程序(内联事件处理程序),请将其替换为null或返回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
        });
    

    您使用的是哪个版本的jquery?
    这个
    在您使用的构造中,dom元素不是jquery对象,因此没有
    解除绑定
    方法。我没有使用绑定。有些图像有onmouseover属性,我想删除它们。我尝试了$('img')。removeAttr('onmouseover'),但仍然不起作用,非常感谢$在我的例子中,ReaveFATR(“OnMouSeOver”)工作了,但是我会考虑内联事件处理程序不是预处理的。+ 1,但是有一个理由将函数赋值给<代码> OnMouthOver < /COD>属性,而不是<代码> null < /Cord>?@ AMNOTiAM,您可以分配null,但是<代码>函数(){false false }。
    是我知道如何做的方式。@karaxuna我从来不知道
    removeAttr()
    删除处理程序。现在我学到了一些新东西:)
    $('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
        });