javascript 从点击处理程序返回 false 在 Firefox 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5422770/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:09:34  来源:igfitidea点击:

Returning false from click handler doesn't work in Firefox

javascripteventsfirefoxcross-browser

提问by Evgeny A.

In the example below, return falsedoes not seem to prevent the default action after the link is clicked (because the page scrolls to the top) in Firefox 3.6 or Chrome 10 but works in Internet Explorer.

在下面的示例中,return false在 Firefox 3.6 或 Chrome 10 中单击链接后似乎不会阻止默认操作(因为页面滚动到顶部),但在 Internet Explorer 中有效。

Using event.preventDefault()does what I need, but I'm wondering why return falsedoes not work with the others.

使用event.preventDefault()可以满足我的需要,但我想知道为什么return false不能与其他人一起使用。

Side note:I do not need to support Internet Explorer.

旁注:我不需要支持 Internet Explorer。

<script>
  addEventListener("DOMContentLoaded", function(){
    document.getElementById("link").addEventListener("click", function(){
      alert("Clicked!");
      return false;
    }, false);
    alert("Click handler bound!");
  }, false);
</script>

<div style="margin-top: 1200px;">
  <a id="link" href="#">Click me!</a>
</div>

回答by Tim Down

return falseworks cross browser, but onlyfor event handlers assigned the "DOM0" way, such as

return false跨浏览器工作,但适用于分配“DOM0”方式的事件处理程序,例如

document.getElementById("link").onclick = function() {
    alert("Clicked!");
    return false;
};

For event handlers assigned the DOM Level 2 way via addEventListener(), you have to use preventDefault():

对于通过 分配 DOM 级别 2 方式的事件处理程序addEventListener(),您必须使用preventDefault()

document.getElementById("link").addEventListener("click", function(evt) {
    alert("Clicked!");
    evt.preventDefault();
}, false);

For event listeners attached via attachEvent()in IE, either return falseor window.event.returnValue = falsewill do:

对于通过attachEvent()IE 中附加的事件侦听器,要么return falsewindow.event.returnValue = false将执行以下操作:

document.getElementById("link").attachEvent("onclick", function() {
    alert("Clicked!");
    return false;
});

回答by Shaz

You say "preventDefault()helps" but you say it doesn't work? I'm not sure I understand. Have you tried this:

你说“有preventDefault()帮助”但你说它不起作用?我不确定我是否理解。你有没有试过这个:

addEventListener("DOMContentLoaded", function() {
    document.getElementById("link").addEventListener("click", function(e) {
        alert("Clicked!");
        e.preventDefault();
        return false;
    }, false);
    alert("Click handler bound!");
}, false);

回答by Brynn

I have had some cross-browser issues with the return false method.

我在 return false 方法方面遇到了一些跨浏览器问题。

I have had good luck just replacing the href of the like with the following:

我很幸运,只是将类似的 href 替换为以下内容:

function removeLink(elementName) {
   var o = document.getElementById(elementName);
   o.href = 'javascript: (function(){})();';
   o.target = "_self";
}

I have found that this empty function in the href property has been the easiest.

我发现 href 属性中的这个空函数是最简单的。

I have added the target _self part because when I develop I occasionally have these links (that I want to be hover boxes) open in a new window/tab. They need to be pointed at the current window to truly do nothing ... lol.

我添加了目标 _self 部分,因为当我开发时,我偶尔会在新窗口/选项卡中打开这些链接(我想成为悬停框)。他们需要指向当前窗口才能真正什么都不做......大声笑。

Hope this helps whomever.

希望这对任何人都有帮助。

Brynn

布林