当 ctrl+ 时,防止当前窗口跟随 onlick/.click() 链接或中键单击

发布于 2024-12-10 18:32:59 字数 454 浏览 0 评论 0原文

我正在使用 onclick 事件使整个 div 成为可导航链接。这很有效,直到您使用 ctrl+单击或单击鼠标中键在新选项卡中打开目标。我发现确实打开了一个新选项卡,但当前选项卡也是如此。

通过 ctrl+单击此处的其中一个框来了解我的意思: http://mw.modhistory.com/ download-1

我不想因为总是强制进入新选项卡而中断正常浏览的能力...用户应该在此处拥有该选项。但我希望能够在新选项卡中快速打开其中的十几个链接,而不必反复“返回”。

我有一个我认为使用 PreventDefault() 的绝妙主意,但这最终与我的想法相反,并且完全阻止了打开新选项卡的能力(一旦我真正考虑过它是什么,这是有道理的打算这样做。)

有什么想法吗?谢谢!

——弗利格

I'm using an onclick event to make an entire div a navigable link. This works well, until you use ctrl+click or middle-mouse click in order to open the target in a new tab. I find that a new tab does indeed open, but so does the current tab.

See what I mean by ctrl+clicking on one of the boxes here: http://mw.modhistory.com/download-1

I don't want to interrupt the ability to browse normally by always forcing it into a new tab...the user should have the option here. But I'd like to be able to quickly open a dozen of those links in new tabs without having to go "back" repeatedly.

I had what I thought was a brilliant idea to use preventDefault(), but that ended up doing the opposite of what I had in mind and entirely prevented the ability to open a new tab (which makes sense once I really thought about what it is meant to do.)

Any ideas? Thanks!

--Fligg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黄昏下泛黄的笔记 2024-12-17 18:32:59

我假设您的事件侦听器正在将事件发送到路由/分配函数,或者您希望实现的点击事件并不适合此功能。

无论哪种方式,您都可以执行以下操作:

// Assuming we already know what the purpose of the click was, and its target
function (e) {
    if (!!e.ctrlKey) { /* CTRL was held during mouse-click */ }
    else { /* not held */ }
}

至于鼠标按钮,它们非常混乱。

您应该可以使用以下任一方法:

e.which /* 1-based: middle-button is #2 */

e.button /* 0-based: middle-button is #1 */

请记住,过去多年来对确定哪些按钮是哪些按钮的支持确实很糟糕。
如果您有一个可以进行抽象的库,请使用它。
否则,除了向使用较新浏览器的人提供此功能,或编译which和button的先前值之外,它最终会解析浏览器版本,这永远不会很好。

I'm assuming that either your event listener is sending events off to a routing/assigning function, or there aren't a whole lot of click events that you're looking to implement that AREN'T for this feature.

Either way, you can do something like:

// Assuming we already know what the purpose of the click was, and its target
function (e) {
    if (!!e.ctrlKey) { /* CTRL was held during mouse-click */ }
    else { /* not held */ }
}

As for mouse-buttons, they're pretty messed.

You should be fine using either:

e.which /* 1-based: middle-button is #2 */

or

e.button /* 0-based: middle-button is #1 */

Keep in mind that past support for figuring out which buttons were which was REALLY bad over the years.
If you've got a library that does the abstracting, use that.
Otherwise, other than offering this functionality to people with newer browsers, or compiling the previous values of which and button, it ends up at parsing browser versions, which is never, ever good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文