JavaScript:点击并返回假

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

JavaScript: onclick and return false

javascript

提问by Chama

I'm using this in my HTML:

我在我的 HTML 中使用它:

 <a href="#" onclick="preload('12345');return false;">Click</a>

It calls the function preload() on an external js-file and works fine so far.

它在外部 js 文件上调用函数 preload() 并且到目前为止工作正常。

But i have dozens of those links and would like to remove alle those "return false" and put only one directly inside the preload()-function in the js-file.

但是我有几十个这样的链接,并且想删除所有这些“返回假”,并只将一个直接放在 js 文件的 preload() 函数中。

But it will always be ignored?! Does the "return false" really only work inside the onclick="..."?

但是会一直被忽略?!“返回假”真的只在 onclick="..." 内有效吗?

回答by Dmitriy

function preload () {
    // some code
    return false;
}

<a href="#" onclick="return preload('12345');">Click</a>

or use addEventListener

或使用 addEventListener

For example:

例如:

<a href="#" class="link">Click</a>
<script type="text/javascript">
    document.querySelector('.link').addEventListener('click', function (e) {
        // some code;

       e.preventDefault();
    }, false);
</script>

回答by Thriggle

Putting return false;in the inline onclickattribute prevents the default behavior (navigation) from occurring. You can also achieve this by clobbering the onclickattribute in JavaScript (i.e. assigning the .onclickproperty to be a function that returns false), but that's frowned upon as old-fashioned and potentially harmful (it would overwrite any additional event listeners attached to that event, for example).

return false;在内嵌onclick属性防止默认行为(导航)的发生。您也可以通过破坏onclickJavaScript 中的.onclick属性来实现这一点(即将属性分配为返回 false 的函数),但由于过时且可能有害(它会覆盖附加到该事件的任何其他事件侦听器,对于例子)。

The modern way to prevent the <a>element's default click behavior from occurring is simply to call the .preventDefault()method of the triggering event from within the attached event listener. You can attach the listener the standard way, using .addEventListener()

防止<a>元素的默认单击行为发生的现代方法是.preventDefault()从附加的事件侦听器中调用触发事件的方法。您可以使用标准方式附加侦听器.addEventListener()

Some examples:

一些例子:

// this works but is not recommended:
document.querySelector(".clobbered").onclick = function() {
  return false;
};

// this doesn't work:
document.querySelector(".attached").addEventListener("click", function() {
  return false;
});

// this is the preferred approach:
document.querySelector(".attachedPreventDefault").addEventListener("click", function(e) {
  e.preventDefault();
});
<a href="/fake" onclick="return false;">If you click me, I don't navigate</a><br/>
<a class="clobbered" href="/fake">If you click me, I don't navigate</a>
<br/>
<a class="attached" href="/fake">If you click me, I navigate</a>
<br/>
<a class="attachedPreventDefault" href="/fake">If you click me, I don't navigate</a>

回答by Don Wayne

try to put the return false clause into inline function

尝试将 return false 子句放入内联函数中

<input onclick="yourFunction();return false;">

回答by ?mer

I think if you put it into the preloadfunction and in the onclickevent just put return falsewill work. Maybe you've tried this code?

我认为如果你把它放入preload函数中,并且在这种情况onclick下 putreturn false会起作用。也许您已经尝试过此代码?

<a href="#" onclick="return preload('12345');">Click</a>

回答by James Taylor

I would maybe suggest not using onClick() and instead using similar jQuery.

我可能会建议不要使用 onClick() 而是使用类似的 jQuery。