![](/img/trans.png)
[英]Hide table row if every cell except first contains a space/is empty
[英]Click on table row except first cell
我有一個html表,其中行和列動態添加。
創建動態行
var row = tableHolder.insertRow(0);
row.id = "row_0";
創建一個單元格
var cell = row.insertCell(0);
cell.id = "cell_0"
現在我需要在動態創建的行上附加一個click事件。
row.addEventListener('click', function(evnt) {
openPopup();
}, false);
單擊該行將打開一個彈出窗口。
現在我想要當用戶點擊每一行的第一個單元格時,不應該觸發click事件並且彈出窗口不會打開。
您可以在jquery中使用:gt選擇器在其他行上添加click事件。
查看以下鏈接: http : //api.jquery.com/gt-selector/
如果你有一個id="mytable"
的表,那么你可以使用jQuery的on()
方法來監聽表中發生在除每行中第一個單元格之外的任何單元格上的事件。 綁定到表的原因是動態創建的行仍將觸發事件(因為事件將冒泡到表)。
// Listen for click event on anything but first cell in each row within #mytable
$('#mytable').on('click', 'td:gt(0)', function() {
alert('Got click on anything but the first cell');
});
// Dynamically create row for demonstration
$('#mytable').append('<tr><td>Cell 1</td><td>Cell 2</td></tr>');
這是一個小提琴: jsfiddle單擊單元格1,您將發現沒有事件被觸發,單擊第二個單元格,將出現警報。
試試這個JQuery解決方案
$('table tr td:not(:first-child)').click(function () {
openPopup();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.