簡體   English   中英

如何在jQuery中使用“ctrl + click”而不是重定向當前選項卡打開新選項卡?

[英]How to open new tab with “ctrl+click” instead of redirect current tab, in jQuery?

表包含數字值,每個單元格都有自己的href

如果我應用這樣的hrefs:

nTd.click(function(e){
   $(this).css("font-weight","bold");
   e.stopPropagation();
   location.href = 'http://google.com';
});

每次單擊單元格重定向窗口, 我都無法通過“ctrl + click”打開新標簽。

如果我在TD添加類似'<a href="http://google.com"> 123123 </a>' ,那么按數字值排序將會打破字典順序。

發生事件時檢查CTRL鍵是否關閉:

nTd.click(function(e){
   $(this).css("font-weight","bold");
   e.stopPropagation();
    if(e.originalEvent.ctrlKey){
        window.open("http://www.google.com", "_blank");
    } else {
        location.href = 'http://google.com';   
    }
});

的jsfiddle

您不會在小提琴中看到頁面更改,但您將在控制台中看到它產生的錯誤。

離開最初的問題我的答案是:不需要jquery你可以簡單地改變你的錨標記以使特征target='_blank' 所以完整的例子是:

<a href="http://google.com" target="_blank"> 123123 </a>

編輯更多評論和另一個想法 :或者您可以將此添加到您的功能:

window.open('url to open','window name','attribute1,attribute2')

所以一個真正的例子就是:

nTd.click(function(e){
   $(this).css("font-weight","bold");
   e.stopPropagation();
   window.open("http://www.google.com", "_blank");
});

錨標記參考 Javascript打開新窗口參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM