jQuery e.preventDefault() 的普通 JavaScript 版本;

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

Plain JavaScript version of e.preventDefault();

javascriptjquery

提问by Ricardo Zea

With the following jQuery based script you can stop the default action of dummy links, ie: <a href="#">

使用以下基于 jQuery 的脚本,您可以停止虚拟链接的默认操作,即: <a href="#">

$('a').click(function(e) {
  e.preventDefault();
});

What would be the plain vanilla version of this script?

这个脚本的普通版本是什么?

I'm not a JavaScript programmer but I'm thinking it may be something that uses return false;. Again, I may be totally wrong.

我不是 JavaScript 程序员,但我认为它可能是使用return false;. 再说一次,我可能完全错了。

Thanks in advance for your help.

在此先感谢您的帮助。

回答by PSL

You have event.preventDefault()available in vanilla javascript as well. In a generic way you can always use return false. If you attach an event handler you are guaranteed to get event agument passed in the handler as opposed to using the onclickor any other attributes of the element (In which case you should rely on the specific event object available in side the handler which you may not get in all browsers, like in IE you would use window.event).

您也可以在 vanilla javascript 中使用event.preventDefault()。以通用方式,您始终可以使用return false. 如果您附加一个事件处理程序,您可以保证在处理程序中传递事件参数,而不是使用onclick元素的或任何其他属性(在这种情况下,您应该依赖处理程序中可用的特定事件对象,而您可能不会进入所有浏览器,就像在 IE 中一样,你会使用window.event)。

Ex: -

前任: -

  document.getElementById('someId').addEventListener('click', function(e){ //say this is an anchor
         //do something
        e.preventDefault();
   });

So for all the anchors:

所以对于所有的锚点:

 var anchors = document.getElementsByTagName('a');
 for(i=0, len=anchors.length; i<len; i++){
     anchors[i].addEventListener('click', function(e){e.preventDefault();});
 }

回答by Mike Samuel

// Like $('a'), gets all the <a> elements in the document.
var aElements = document.getElementsByTagName('a');
// Create one function object instead of one per <a> element.
// The calling convention is the same for jQuery as for regular JS.
function preventDefaultListener(e) { e.preventDefault(); }
// For each a element,
for (var i = 0, n = aElements.length; i < n; ++i) {
  // register the listener to be fired on click.
  aElements[i].addEventListener('click', preventDefaultListener);
}

回答by Drixson Ose?a

yes you can use return false;

是的,你可以使用 return false;

<a href="" onclick="return false;">test</a>

回答by Yuriy Galanter

Return false is the way to go. E.g. if your anchor already has an href:

返回 false 是要走的路。例如,如果您的锚点已经有一个 href:

<a href="http://www.google.com" onclick="alert('clicked!');return false">Click me</a>

calling onclick like this will only perform the function, but not the navigation.

像这样调用 onclick 只会执行功能,而不是导航。

回答by Zone6

@Ricardo you are correct sir.

@Ricardo 你是对的,先生。

Not only does return falsedisable the default action for a said element, it prevents bubbling also.

不仅return false禁用所述元素的默认操作,还可以防止冒泡。

 e.preventDefault();
 e.stopPropagation();

is equal to

等于

return false;

return false;

you decide :)

你决定 :)