JavaScript 点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7868830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:34:41  来源:igfitidea点击:

JavaScript click event

javascript

提问by SimonNN

My browser (firefox) prevents any popup from loading, and loads links that open new windows in the current tab, unless I explicitly say I want the link to load on a new tab or window, with the appropriate shortcuts (for example, middle click on the link, or left click with ctrlpressed causes the link to open on a new tab, and shift+ left click on a new window).

我的浏览器 (firefox) 阻止加载任何弹出窗口,并加载在当前选项卡中打开新窗口的链接,除非我明确说明我希望使用适当的快捷方式将链接加载到新选项卡或窗口上(例如,中键单击在链接上,或ctrl按住左键单击会导致链接在新选项卡上打开,shift然后在新窗口上左键单击)。

I would like to create a javascript function f()that runs some code (meant to create the link address) when the link is pressed, and then loads the link that has been created, without removing the user experience described above. Right now what I have is something like <a href="javascript:void(0)" onclick="f()"/>, but middle click doesn't work (it instead loads the url javascript:void(0)) and neither do the other features described above.

我想创建一个 javascript 函数f(),该函数在按下链接时运行一些代码(用于创建链接地址),然后加载已创建的链接,而不会删除上述用户体验。现在我所拥有的类似于<a href="javascript:void(0)" onclick="f()"/>,但是中键单击不起作用(而是加载 url javascript:void(0))并且上述其他功能也不起作用。

Do you have any idea as how to solve my problem ?

你知道如何解决我的问题吗?

Thanks.

谢谢。

回答by LordZardeck

have you tried window.open('url')?

你试过 window.open('url') 吗?

see: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml

见:http: //www.javascript-coder.com/window-popup/javascript-window-open.phtml

Also, as far as I know, you can't control whether or not the browser opens in a new tab or new window. That is a browser setting that is different for every user.

另外,据我所知,您无法控制浏览器是否在新标签页或新窗口中打开。这是每个用户都不同的浏览器设置。

You might also try removing the onclick, and using <a href="javascript:f()"></a>

您也可以尝试删除 onclick,并使用 <a href="javascript:f()"></a>

EDIT

编辑

There seems to be issues with using middle click with opening new tabs instead of executing the javascript: middle click (new tabs) and javascript links

使用中键打开新标签而不是执行 javascript 似乎存在问题:中键(新标签)和 javascript 链接

As that site says, you can instead create an id for the element and bind it through javascript.

正如该站点所说,您可以改为为元素创建一个 id 并通过 javascript 绑定它。

**Taken from that link:

**取自该链接:

<a href="/non/ajax/display/page" id="thisLink">...</a>

And then in your JS, hook the link via it's ID to do the AJAX call. Remember that you need to stop the click event from bubbling up. Most frameworks have an event killer built in that you can call (just look at its Event class).

Here's the event handling and event-killer in jquery:

$("#thisLink").click(function(ev, ob) {
    alert("thisLink was clicked");
    ev.stopPropagation();
});
<a href="/non/ajax/display/page" id="thisLink">...</a>

然后在您的 JS 中,通过它的 ID 挂钩链接以执行 AJAX 调用。请记住,您需要阻止点击事件冒泡。大多数框架都有一个内置的事件杀手,你可以调用它(看看它的 Event 类)。

这是 jquery 中的事件处理和事件杀手:

$("#thisLink").click(function(ev, ob) {
    alert("thisLink was clicked");
    ev.stopPropagation();
});

Without jQuery, it might look like this:

如果没有 jQuery,它可能看起来像这样:

document.getElementById('thisLink').onclick = function(e)
{
     //do someting
     e.stopPropagation();
}

回答by Neil

Other browsers may vary, but by default Firefox doesn't tell the web page that it has been middle-clicked (unless you set the hidden preference to enable the feature). You might be able to create a workaround based on the focusand/or mouseoverevents instead.

其他浏览器可能会有所不同,但默认情况下 Firefox 不会告诉网页它已被中键单击(除非您设置隐藏首选项以启用该功能)。您或许可以创建基于focus和/或mouseover事件的解决方法。