2013-02-22 189 views
0

我確實遇到了很大的麻煩。我無法得到。 我確實有href =「#錨點」鏈接不能與可點擊的jQuery div以外

<div id="clickable"> 
    <a href="http://domain.de/link.html">link1</a> 
    <a href="http://domain.de/link2.html">link2</a> 
    <a href="http://domain.de/me.html#options">anchor link</a> 
</div> 

jQuery('#clickable').click(function() { 
    window.location.href='http://domain.de/link.html'; 
}); 

1和連接2工作正常,但錨鏈接調用的div單擊事件

我已經嘗試了所有關於這裏冒泡的DOM上的計算器 既不event.stopPropagation(); return false;event.preventDefault();正在答案

只有event.stopPropagation();我確實有因爲沒有什麼相同的效果上述

+0

給錨了'顯示:塊;'和使用'stopPropagation()' – Ohgodwhy 2013-02-22 12:43:32

+0

不工作,依然是同樣的問題 – Viktor 2013-02-22 12:48:46

+0

你可以e'加上'的處理程序,並做'如果($(! e.target).is('#clickable'))return;'。 – 2013-02-22 12:51:21

回答

1

所看到您需要防止點擊活動,以泡泡您的<a>項目的父母。例如:

<div id="clickable"> 
    <a href="http://domain.de/link.html">link1</a> 
    <a href="http://domain.de/link2.html">link2</a> 
    <a href="http://domain.de/me.html#options">anchor link</a> 
</div> 

jQuery('#clickable a').click(function(event){ 
    event.stopPropagation(); 
}); 

jQuery('#clickable').click(function() { 
    window.location.href='http://domain.de/link.html'; 
});