2016-01-13 53 views
2

任何人都可以讓我知道如何禁用特定列的點擊事件。限制特定列的點擊事件

場景:我們在表格中顯示用戶詳細信息,一旦對錶格行進行了單擊,彈出對話框窗口將顯示更多詳細信息(調用Ajax請求以從數據庫中檢索詳細信息)。但我們的約束是禁用與表關聯的單列的單擊事件。

如:

<table border = '1'> 
<tr> 
<th> Name </th> 
<th> Id </th> 
<th> Phone Number</th> 
</tr> 
<tr onclick = "testing()"> 
<td> Krupa </td> 
<td> 123 </td> 
<td> <a href = "http://www.google.com" target= '_blank'>Click me </a> </td> 
</tr> 
</table> 

如果點擊已取得的文字(第1和第2列),它會調用上單擊事件。但是,如果用戶點擊超鏈接(第三列),我想重定向到谷歌而不是點擊事件(測試())。

誰能幫我實現這個

回答

1

試試這個:

//snip 
<tr> 
<td onclick = "testing()"> Krupa </td> 
<td onclick = "testing()"> 123 </td> 
<td> <a href = "http://www.google.com" target= '_blank'>Click me </a> </td> 
</tr> 
//snip 
1

試試這個

一個名爲templatecell類添加到相應的TD,以防止點擊。

<table border = '1'> 
<tr> 
<th> Name </th> 
<th> Id </th> 
<th> Phone Number</th> 
</tr> 
<tr onclick = "testing()"> 
<td> Krupa </td> 
<td> 123 </td> 
<td class="templatecell"> <a href = "http://www.google.com" target= '_blank'>Click me </a> </td> 
</tr> 
</table> 

腳本是這樣的

$("table").on("click","td", function(e){ 
    if($(e.target).closest(".templatecell").length){ 
    //Clicked hyper link 
    //do action and return from here 
    return; 
    } 
    //Else clicked on td cell show popup 
}) 
0
function testing() { 

     alert('testing') 
    } 
    $(document).ready(function() { 
     $('table tr td').click(function() { 
      if ($(this).index() < 2) { 
       testing(); 
      } 
      else { 
      // window.open($(this).find('a').attr('href'));  //working 

       $(this).find('a')[0].click(); 
      } 
     }); 

});