2013-05-08 52 views
0

我試圖阻止父點擊操作時點擊子元素jQuery的防止兒童onclick事件家長行動

我的JavaScript代碼:

$(".profile").bind("mouseenter mouseleave click",function(e){ 
e.stopPropagation(); 
e.preventDefault(); 
//if blocks depending of one of three actions 
} 

這.profile文件元素中是DIV與列表

<div id="tooltip-content" class="tooltip center" style="display: block;"> 
     <ul> 
      <li id="name">Toms IrForšs </li> 
      <li id="main"><a onclick="something()">1</a></li> 
      <li id="remove"><a onclick="something()">2</a></li> 
      <li id="sendmail"><a onclick="something()">3</a></li> 
     </ul> 
</div> 

但是當某些方法被調用時父觸發事件也被觸發。

+1

「.profile」元素究竟在哪裏? – 2013-05-08 07:24:31

+0

你需要''something()'函數裏面的stopPropagation()' – billyonecan 2013-05-08 07:33:26

+0

是的,很好的問題......你的代碼沒有描述你的問題。 – Andreyco 2013-05-08 07:35:21

回答

2

試試這個

$(".profile").bind("mouseenter mouseleave click",function(e){ 
    if (e.target == this) 
    { 
     //do your stuff 
    } 
}); 

這將執行的代碼if條件裏面只有當點擊的元素是被綁定在相同的元素。

,並使用on()代替bind()

+0

謝謝。這招伎倆 – DoubleT 2013-05-08 07:39:34

+0

歡迎..很高興幫助... – bipen 2013-05-08 07:56:20

0

我創建了搗鼓你,有概念,應該爲你(link)在你的代碼變化不大的工作。

你想念的是取消傳播 - 使用stopImmediatePropagation方法。

就是這樣!