2011-01-19 29 views
1

我面臨一個奇怪的情況。點擊我綁定的事件將調用一個AJAX函數,但是當我按下Ctrl單擊默認點擊操作應該被禁用。有什麼辦法,我可以實現它..請就如何進一步進行任何想法..在Ctrl上禁用單擊操作單擊

$('#demo1').click(function(e) { 
    if(e.ctrlKey == true) { 
    alert("control click"); 
    } 
    else { 
    alert("normal click"); 
     $("#demo1").bind('select_node.jstree', function(event, data) { 
     var url = data.rslt.obj.children("a:eq(0)").attr("href"); 
     $('.ui-layout-center').load(url); 
     }); 
    } 
}); 
+0

我能夠做到這一點,但jstree綁定點擊仍然啓動...看到修改後的代碼... – Sullan 2011-01-19 18:12:03

回答

2

檢查e.ctrlKey

+0

只檢查e.ctrlKey不會幫助,你應該檢查e.ctrlKey ||如果在mac中按下命令鍵,e.metaKey作爲metaKey屬性設置爲true – Amareswar 2011-05-16 05:20:18

0

我絕對不是那個熟悉的(只是最近我纔在jQuery的開始),但會的preventDefault和http://plugins.jquery.com/plugin-tags/ctrl-click幫助你呢?

+0

event.preventDefault或preventPropogation?可能有幫助。當我有機會時,我會嘗試一下。 – gbvb 2011-01-20 14:56:08

1
if (e.ctrlKey || e.metaKey) 
{ 
    /* do your click thing (e.g. open-in-new-window) */ 
    window.open($(this).attr('data-url')); 
    return; 
} 
else 
{ 
    /* do your regular thing (e.g. go to another page) */ 
    window.location.href = $(this).attr('data-url'); 
} 

爲綁定部分,也許e.preventDefault();幫助?

並且:在上面的代碼中嘗試使用返回false,以防止事件「冒泡」。

+0

你還應該考慮shift鍵,就像[這個答案](http://stackoverflow.com/a/25919826/421243) – 2015-08-24 01:49:19