PreventDefault alternative for IE8

南笙酒味 提交于 2019-11-27 16:02:44

I use something like:

(event.preventDefault) ? event.preventDefault() : event.returnValue = false; 

the event.returnValue property is the closest IE equivalent to preventDefault.

Using

return false;

can sometimes also work, but it can lead to unexpected behavior sometimes when mixed with e.g. jQuery (jQuery also does stopPropagation...which is usually what you want, but...), so I prefer not to rely on it.

IE8 does not support preventDefault; it has returnValue instead. jQuery should normalize that for you, though. Are you sure you are calling preventDefault on the jQuery event wrapper (and not the actual event object)?

Just use

return false;

it's cross browser and has the same purpose as event.preventDefault();

THe same instruction in jQuery is slightly different, it includes also stopPropagation().

Use

$('.selector').click(function(event) {event.preventDefault();

jquery docs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!