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. 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.

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. 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

This code from a comment is probably the best for you:

if (event.preventDefault) { 
  event.preventDefault(); 
} else { 
  event.returnValue = false; 
} 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top