2010-12-10 35 views
2

我有一個作爲ui-widget-h3作爲ui-widget-header的ui-widget-content的div。標題有三個按鈕。分別具有「保存」,「刪除」和「取消」的類別。我想在你點擊div時有一個.click功能,除非你真的點擊了其中一個按鈕。jquery/jqueryui,點擊div除了按鈕之外的任何地方

我想:$(".record").not(".save").not(".delete").not(".cancel").click(

我的代碼是:

<div class="record cursorPointer hoverClass ui-widget-content"> 
    <h3 class="ui-widget-header"> 
     <table width="100%"><tr> 
      <td align="left" style="width:33%; color:Red">somecontractorone</td> 
      <td style="width:33%" align="center"> Name: Joe Q Shmoe </td> 
      <td style="width:33%" align="right"> Buisness: joeqshmoeelectrical</td> 
      <td><button style="width:20px;height:20px" class="save"></button></td> 
      <td><button style="width:20px;height:20px" class="delete"></button></td> 
      <td><button style="width:20px;height:20px" class="cancel"></button></td></tr> 
     </table> 
    </h3> 
</div> 

但是點擊功能點擊按鈕時仍然激活。

任何想法?

回答

9

您可以使用event.target來查看點擊的內容。然後依靠冒泡並在父母div上附加點擊處理程序。 jsfiddle

$('.record').click(function(e){ 
    if($(e.target).is(':button')) 
     alert('button'); 
    else 
     alert('not button'); 
}); 
+0

'$(「。record」)。click(function(e){!$(e.target).is(':button')){'works。但我不知道它的工作原理和我做的方式不... – kralco626 2010-12-10 17:48:24

+1

@ kralco626 - 嘗試jsfiddle的例子,如果你仍然有問題。 – 2010-12-10 17:51:11

+0

我知道它與e.target一起工作。只是不知道爲什麼它不符合我的方式,但那沒關係。謝謝! – kralco626 2010-12-10 18:23:00

2

假設你有一個處理的按鈕另一個事件處理程序(或幾個),可以防止在按鈕的點擊處理程序(S)在與

$('button').click(function(e) { 
    e.stopPropagation(); 
    switch(this.className) { 
     // Or whatever... 
    } 
}) 
相關問題