简体   繁体   English

为跨浏览器event.preventDefault();修改Event.prototype是否是个坏主意?

[英]Is it a bad idea to modify Event.prototype for cross-browser event.preventDefault();?

Pretty straightforward question. 很简单的问题。 < IE8 doesn't support event.preventDefault() , so I'd like to modify Event.prototype for IE to add my own preventDefault method utilizing event.returnValue . <IE8不支持event.preventDefault() ,所以我想修改IE的Event.prototype以使用event.returnValue添加我自己的preventDefault方法。 Simple task, but is it a bad idea? 简单的任务,但这不是一个好主意吗?

So far, in my opinion, the best answer to this question is that it's not a bad idea. 在我看来,到目前为止,对这个问题的最佳答案是这不是一个坏主意。 That doesn't mean screwing around with prototype in general or even with other methods of Event is a good idea, but normalizing event.preventDefault() seems entirely harmless– no, helpful. 这并不意味着一般地使用原型甚至是Event其他方法都是个好主意,但是规范化event.preventDefault()似乎完全没有害处-不,没有帮助。

Please do chime in if you can provide a better answer. 如果您可以提供更好的答案,请进行提示。

You should never manipulate native code like that. 您永远都不应像这样操纵本机代码。 You should have code that does feature detection and does proper behavior. 您应该具有能够进行功能检测并具有正确行为的代码。

If you manipulate browser prototype you run the risk of conflicting with plugins because no authors are expecting that. 如果您操纵浏览器原型,则会冒与插件冲突的风险,因为没有作者期望这样做。 For example a chrome plugin overwrites a certain property which breaks TinyMCE. 例如,chrome插件会覆盖某个破坏TinyMCE的属性。 It's quite frustrating. 真令人沮丧。

It's the web which is not a tight box so you can't pretend only you are using the browser. 它不是一个封闭的网络,因此您不能只假装正在使用浏览器。

This question shows you the right way already: 这个问题已经向您显示了正确的方法:

event.preventDefault() function not working in IE event.preventDefault()函数在IE中不起作用

This code from a comment is probably the best for you: 注释中的以下代码最适合您:

if (event.preventDefault) { 
  event.preventDefault(); 
} else { 
  event.returnValue = false; 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM