简体   繁体   English

单击第一个单元格以外的表格行

[英]Click on table row except first cell

I have a html table in which rows and columns are adding dynamically. 我有一个html表,其中行和列动态添加。

Create dynamic row 创建动态行

var row = tableHolder.insertRow(0);
row.id = "row_0";

Create a cell 创建一个单元格

var cell = row.insertCell(0);
cell.id = "cell_0" 

Now I need to attach a click event on the dynamically created row. 现在我需要在动态创建的行上附加一个click事件。

row.addEventListener('click', function(evnt) {
    openPopup();    
}, false);

A popup will open on click on the row. 单击该行将打开一个弹出窗口。

Now I want when user click on first cell of every row, click event should not be fired and popup will not open. 现在我想要当用户点击每一行的第一个单元格时,不应该触发click事件并且弹出窗口不会打开。

You can use :gt selector in jquery to add click event on other rows. 您可以在jquery中使用:gt选择器在其他行上添加click事件。

Checkout the following link : http://api.jquery.com/gt-selector/ 查看以下链接: http//api.jquery.com/gt-selector/

If you have a table with id="mytable" then you can use jQuery's on() method to listen for events on the table which occur on any cell except the first one in each row. 如果你有一个id="mytable"的表,那么你可以使用jQuery的on()方法来监听表中发生在除每行中第一个单元格之外的任何单元格上的事件。 The reason for binding to the table is so that dynamicly created rows will still trigger the event (since the event will bubble up to the table). 绑定到表的原因是动态创建的行仍将触发事件(因为事件将冒泡到表)。

// 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>');

Here's a fiddle: jsfiddle Click on cell 1 and you will find no event fired, click on the second cell and the alert will appear. 这是一个小提琴: jsfiddle单击单元格1,您将发现没有事件被触发,单击第二个单元格,将出现警报。

Try this JQuery Solution 试试这个JQuery解决方案

$('table tr td:not(:first-child)').click(function () {
        openPopup();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM