2016-09-15 27 views
-2

我有一個HTML表所示:爲什麼我的html表格忽略點擊?

<table id="top10ItemsTable"> 
    <tr> 
     <th>Item Code</th> 
     <th>Description</th> 
     <th class="rightjustifytext">Qty</th> 
    </tr> 
    . . . 

我想響應點擊該表內,因此已將此添加到ready函數:

$("body").on("click", "#top10ItemsTable", function() { 
    alert('you clicked inside the Top 10 Items quadrant table'); 
}); 

......但擊html表格不會產生警報,除非它是微觀的或不可見的。

需要什麼捅桌子足夠堅硬,以得到它的皮屑?

+3

看起來工作得很好^ h ttps://jsfiddle.net/rb1hyxh2/19/ –

+0

你對任何包含該表的元素都有一個點擊處理程序嗎?該處理程序是否使用'event.stopPropagation'或'return false;'? – Barmar

+0

請參閱http://stackoverflow.com/questions/39301652/delegated-event-binding-in-jquery/39301740#39301740對此的解釋。 – Barmar

回答

2

你可能只是做:

$("#top10ItemsTable").click(function() { 
    alert('you clicked inside the Top 10 Items quadrant table'); 
} 
1

試試這個:

$(document).on("click", "#top10ItemsTable", function() { 

    alert('you clicked inside the Top 10 Items quadrant table'); 

}) 

最終代碼:

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
</head> 
 
    <body> 
 
     <table id="top10ItemsTable"> 
 
    <tr> 
 
     <th>Item Code</th> 
 
     <th>Description</th> 
 
     <th class="rightjustifytext">Qty</th> 
 
    </tr> 
 
     </table> 
 
     
 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
     <script> 
 
      
 

 
    
 
$(document).on("click", "#top10ItemsTable", function() { 
 
    
 
    alert('you clicked inside the Top 10 Items quadrant table'); 
 
     
 
})  
 
     </script> 
 
    </body> 
 
</html>