我有一个链接,可以在左键单击时进行 ajax 调用,但在 ctrl+单击或中键单击时在新选项卡中打开。
我从这里复制了代码,Ajax link which can also be opened in new tab? (like Facebook)
但它在我的 Firefox 中不起作用。问题是它永远不会触发 ajax 调用,它总是打开链接。
这是html代码:
<a href="http://forma.culturadigital.cc/es/casos" onclick="javascript:unhide('casos', 'nil', 'nil');javascript:activate('menucasos');javascript:return makeGetRequest(event,'http://forma.culturadigital.cc/pages/casos.php');" class="menu_link"><span id="menucasos" class="nonactive"> Casos de estudio </span></a>
这是 ajax 代码:
function makeGetRequest(e, key) {
if (!e) e = window.event;
if (!e.ctrlKey && !e.button == 1) { //this is to be able to open links by clicking the wheel button or ctrl+click
http.open('GET', key, true);
//assign a handler for the response
http.onreadystatechange = processResponse;
//actually send the request to the server
http.send(null);
return false; //this is important, otherwise the href link will trigger and not the onclick
}
}
它在 Chrome 和 IE 中运行良好。但在FF中则不然。实际网站是FORMA
最佳答案
删除 onclick
中的 javascript:
部分。
<a href="http://forma.culturadigital.cc/es/casos" onclick="unhide('casos', 'nil', 'nil');activate('menucasos');return makeGetRequest(event,'http://forma.culturadigital.cc/pages/casos.php');" class="menu_link"><span id="menucasos" class="nonactive"> Casos de estudio </span></a>
前缀 javascript:
用于链接中的 href
属性,在 onclick
处理程序中,您应该放置普通的 javascript。
除此之外,您的 activate
函数中的 js 存在错误。
它开始于:
function activate(fieldID){
var e = window.event;
if (!e.ctrlKey && !e.button == 1) {
对于 Firefox,window.event
返回 undefined
。更改它以将事件作为参数传递,就像在 makeGetRequest
activate(event,'menucasos');
和
function activate(e, fieldID){
if (!e.ctrlKey && !e.button == 1) {
关于javascript - href 链接使用左按钮调用 ajax 并使用中按钮在新选项卡中打开 - 在 FF 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25499418/