IE 7 CTRL +单击打开一个新窗口 - 如何抑制它?
当CTRL+单击链接打开新窗口时,是否可以抑制默认的 IE 7 功能?如果是这样,怎么办?
谢谢!
Is it possible to suppress the default IE 7 functionality when CTRL+click on link opens a new window? if so, how?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Internet Explorer 中,无法抑制 Ctrl + 单击没有子元素的链接 - 如果以下情况,则链接单击根本不会触发
onclick
事件:按住 Ctrl 键。微软似乎不想让你改变这个功能,因为担心你可能会让用户感到困惑。在发布此答案之前,我搜索了某种官方确认/解释,但不幸的是,MSDN 上的文档中未列出此限制,并且 Google 没有帮助。尽管如此,它仍然是正确的,请亲自尝试一下:
您会发现 Ctrl + 单击链接不会引发警报框。根据 pinkgothic 的说法,为链接分配一个子元素可以解决这个问题。例如:
这是有效的,因为点击首先针对
元素触发,然后再传播到
元素。
There is no way to suppress a Ctrl + Click on a link with no child elements in Internet Explorer -- the
onclick
event doesn't fire at all for link clicks if the Ctrl key is held down. It seems that Microsoft don't want you to change this functionality out of fear that you might confuse the user.I searched for some sort of official confirmation/explanation before posting this answer, but unfortunately this limitation is not listed in the documentation on MSDN and Google wasn't helpful. Nevertheless, it remains true, try it yourself:
You will find that a Ctrl + click on the link will not throw the alert box. According to pinkgothic, assigning a child element to the link will work around the problem. For example:
This works because the click is triggered for the
<span>
element first, before propagating to the<a>
element.jQuery event.preventDefault() 方法或类似方法可以覆盖您拥有的页面上的默认行为控制。
在没有充分理由的情况下改变用户浏览器的行为通常是不好的做法,因为浏览器及其行为是“他们的”。
The jQuery event.preventDefault() method or similar can override default behavior on pages that you have control over.
It is generally bad practice to alter the behaviour of a user's browser without really good reason as the browser and its behaviour is "their's".
与 op 有同样的问题,并通过为锚点 href 属性提供一个“#”和一个带有相应链接位置的附加 data-href 属性来解决该问题。
缺点是您需要一个单击处理程序来跟踪链接,而且右键单击“在新窗口中打开”也无法使用此方法。
had the same problem as the op and solved it by giving the anchor href attribute an '#' and an additional data-href attribute with the corresponding link location.
The downside you need a click handler to follow the link, also right clicking "open in new window" wont work with this approach.
将链接文本包裹在一个跨度内。像这样:
wrap your link text inside an span. like this:
将脚本添加到锚点并返回 false
在树视图中使用锚点是有效的,因为它使树视图更易于访问。
Add script to the anchor and return false
And it is valid to use anchors in a treeview because it makes the treeview more accessible.
使用 jquery,您可以自己处理 ctrl+click 功能并返回 false,例如,
这适用于所有浏览器,包括 mac
Using jquery you can handle the ctrl+click functionality for yourself and return false, for example
this works across all browsers including mac