event.preventDefault()或return false在IE9中不起作用 [英] event.preventDefault() or return false don't work in IE9

查看:256
本文介绍了event.preventDefault()或return false在IE9中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让以下代码在所有版本的IE中工作,因为它可以在其他浏览器中找到:

I'm trying to get the following code to work in all versions of IE, as it works find in other browsers:

  <a href="" class="specificClass">Click Me </a>
    //Javascript
     $(".specificClass").click(
                        function(e) {
                            e.preventDefault();
                           // Do Something
                             //return false; Doesn't work either
                        }
                );

切换到 href =#它试图去到页面的顶部,再次只在IE9。例如:
离开 href =在IE9中重定向到当前链接本身。

Switching to href="#" makes it try to go to the top of the page, again only in IE9. For example: Leaving href="" redirects to the current link itself in IE9.

<a href="#" onclick="doSomething(this); return false;"> Click Me Two </a>

看起来这两种方法都会触发onclick Javascript被调用,但是默认行为 href =未被覆盖。如果我使用 event.preventDefault()没有发生任何事情。

It seems like both approaches triggers the onclick Javascript to be called, but the default behavior for href="" is not getting overridden. If I use event.preventDefault() nothing happens.

以下方法适用:

<a href="javascript:doSomething(this);"> Click Me Two </a>
function doSomething(me) {

    // event.preventDefault is not needed as the javascript is added via href

}

但我不想对所有我的锚标签使用href =javascript:或onclick =doSomething以使其在IE9中正常工作。
我也不想使用不同的标签(例如尝试了span标签),因为它是难以在所有浏览器中的样式。

However I don't want to have href="javascript:" or onclick ="doSomething"for all my anchor tags just to get it to work in IE9. I also don't want to use a different tag (tried the span tag for example) since it is tricky to style up in all browsers.

任何其他想法?

看起来这是一个合法的错误, 。我现在还提出了一个解决方法:
https://connect.microsoft.com/IE/feedback/details/812247/event-preventdefault-or-return-false-dont-work-in-ie9

Looks like it is a legit bug, I have submitted a request to fix it. I have also put in a workaround for now: https://connect.microsoft.com/IE/feedback/details/812247/event-preventdefault-or-return-false-dont-work-in-ie9

推荐答案

在IE9中,旧的事件处理程序模型仍然被部分使用。 preventDefault()仅在使用 addEventListener()附加事件侦听器时有效。

In IE9 the legacy event handler model is still partial used. preventDefault() works only, when the event listener is attached using addEventListener().

如果您想要阻止内联处理程序的默认操作,则必须使用旧方法:

If you want to prevent default action from an inline handler, you have to use the legacy method:

event.returnValue = false;
event.cancelBubble = true; // This is affects like event.stopPropagation() in older IEs

虽然jQuery不工作是奇怪的,我没有解释这个...除非你在兼容模式下运行IE并使用jQuery 2.X?

Though jQuery not working is odd, I've no explanation for that... Unless you're running IE in compatible mode and use jQuery 2.X?

EDIT

此外,引用 console ; 10,如果Dev Tools没有打开。你可以找到很多修复这个问题在SO。我最喜欢的是:

Also a reference to console object will break the code in IE<10, if Dev Tools are not opened. You can find a lot of fixes for this problem at SO. My favorite is this:

// The very first lines in the global context
if (!window.console) {
    window.console = {
        log: function () {}
        // Add other console methods, if the scripts on the page are using them
    }
}

虽然 console 问题可以避免使用上面的代码,总是最好从要发布的最终代码中删除所有日志。

Though the console problem can be avoided with the code above, it's always better to remove all loggings from the final code to be published.

这篇关于event.preventDefault()或return false在IE9中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆