【问题标题】:Why does the "onmouseover" event use "return true" to prevent default behavior?为什么“onmouseover”事件使用“return true”来防止默认行为?
【发布时间】:2010-10-04 20:59:50
【问题描述】:

我一直在寻找这个,但没有任何解释。

对于 javascript 中的“onclick”和其他事件,返回 false 的事件处理程序表示“阻止默认操作”。但是,“onmouseover”有一个例外。对于“onmouseover”,返回 true 表示“阻止默认操作”。

为什么“鼠标悬停”会有这么奇怪的例外情况?

【问题讨论】:

    标签: javascript events onmouseover event-handling


    【解决方案1】:

    不要使用 return false / true 来防止默认事件行为,而是使用事件对象的默认方法/属性:

    elem.onmouseover = function(e) {
        if (!e) var e = window.event; // IE
        if(e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false; // IE
        }
    }
    

    【讨论】:

      【解决方案2】:

      据我所知,返回 true 以防止链接的 url 显示在状态栏中是 IE 特定的(错误)功能。试图理解为什么 IE 以它的方式做事往往是一个失败的原因 - 只是忍受它......

      【讨论】:

        【解决方案3】:

        谁知道为什么。就是这样: 您从 onmouseover 返回 TRUE 以防止 url 出现在状态栏中,这确实是默认行为。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-06
          • 1970-01-01
          • 2011-01-03
          • 1970-01-01
          • 1970-01-01
          • 2019-03-31
          • 2023-03-23
          • 1970-01-01
          相关资源
          最近更新 更多