Ajax的链接,也可以在新标签中打开? (如Facebook)标签、链接、Ajax、Facebook

2023-09-10 17:55:41 作者:莫大于心死

如果我只需点击大部分的Facebook的链接将被用ajax同一页上加载,但如果我中/按住Ctrl键单击,将在新标签中打开。

我已经做了一半的方式,但它完全不起作用,中键单击,然后右键单击>开放式的新选项卡中打开的新标签,但按Ctrl键调用的onclick [/编辑]。任何想法如何正确地做到这一点?

 < A HREF =/连接/位置的onclick =doAjax();返回false;>链接< / A>
 

解决方案

而不是总是直接在(内置)返回false的onclick,要返回函数的结果,然后在功能测试是否控制键使用:

 < A HREF =/连接/位置的onclick =返回doAjax(事件);>链接< / A>

<脚本>
功能doAjax(五){
    如果E = window.event(E!);

    如果(!e.ctrlKey){
        警报(阿贾克斯code在这里);
        返回false;
    }
}
< / SCRIPT>
 
ajax

演示: http://jsfiddle.net/DrvYg/

有关鼠标事件的事件对象的属性的详细信息:的https://developer.mozilla .ORG / EN / DOM /的MouseEvent

(顺便说一句,你不应该需要如果(!E)位内联事件处理程序,但那种我已经习惯了写它,因为我不倾向于使用内联事件处理程序。)

If I simply click on most of Facebook link it will be loaded on same page using ajax but if I middle/ctrl-click it will be loaded in new tab.

I have done it half way but it does not work completely, [Edit] middle-click and right-click>open-in-new-tab opens is new tab but ctrl-click calls onclick[/Edit]. Any idea how to do it correctly?

<a href="/link/location" onclick="doAjax(); return false;">link</a>

解决方案

Instead of always returning false directly in the (inline) onclick, you want to return the result of the function, and then in the function test whether the control key was used:

<a href="/link/location" onclick="return doAjax(event);">link</a>

<script>
function doAjax(e) {
    if (!e) e = window.event;

    if (!e.ctrlKey) {
        alert("Ajax code here");
        return false;
    }
}
</script>

Demo: http://jsfiddle.net/DrvYg/

More information about the properties of the event object for mouse events: https://developer.mozilla.org/en/DOM/MouseEvent

(By the way, you shouldn't need the if (!e) bit for an inline event handler, but I'm kind of used to writing it because I don't tend to use inline event handlers.)