简体   繁体   English

使用jQuery,如何让click事件处理程序响应所选的表列?

[英]Using jQuery, how to have click event handler respond for selected table columns?

jQuery v1.11 jQuery v1.11

Given an HTML table with 6 columns, I want the cells in the table in columns two, three, five and six to respond to click events. 给定一个包含6列的HTML表格,我希望表格中第2,3,5和6列的单元格能够响应click事件。 So if a user clicks on a cell in column one or four, the click event handler should not be called. 因此,如果用户单击第一列或第四列中的单元格,则不应调用click事件处理程序。

This prevents the event handler from being called when the user clicks in the first column: 这可以防止在用户单击第一列时调用事件处理程序:

 $('#my-table').on('click', 'tbody td:not(:first-child)', function (e) {
    alert("I've been clicked on!");
 });

And his prevents the event handler from being called when the user clicks in column 4: 并且当用户在第4列中单击时,他会阻止调用事件处理程序:

 $('#my-table').on('click', 'tbody td:not(:nth-child(4))', function (e) {
     alert("I've been clicked on!");
 });

My question is , how do I modify the above so that the event handler is not called when a click occurs in either column one or four. 我的问题是 ,如何修改上述内容,以便在第一列或第四列中发生单击时不调用事件处理程序。

JSFiddle 的jsfiddle

Edit: @micnil answered my specific question and I will find knowing the pattern he suggested useful. 编辑: @micnil回答了我的具体问题,我会发现他知道他建议有用的模式。 However, @Oleg took the time to point out a better approach. 但是,@ Oleg花时间指出了一种更好的方法。 Rather than binding the event handler to each cell, he suggested that I should bind an event handler to the table. 他没有将事件处理程序绑定到每个单元格,而是建议我将事件处理程序绑定到表中。 In my case this proves to be better. 在我的情况下,这被证明是更好的。

Using performance.now() , discussed here , I get the following results setting up the binding for a jQuery DataTable containing 1,000 rows in Chrome: 使用此处讨论的 performance.now() ,我得到以下结果,为Chrome中包含1,000行的jQuery DataTable设置绑定:

 Binding the click event to cells took 0.14627581768183972 milliseconds.

 Binding the click event to the table took 0.04619236347855349 milliseconds.

You can just put a coma inside the selector: 你可以在选择器中放入一个昏迷:

 $('#my-table').on('click', 'tbody td:not(:nth-child(4), :first-child)', function (e) {
     alert("I've been clicked on!");
 });

I think the best choice in your case is to use the JQuery function index() that will give you the index of clicked td and you can do the condition you want based to the returned index, take a look at Your updated fiddle . 我认为在你的情况下最好的选择是使用JQuery函数index() ,它会给你点击td的索引,你可以根据返回的索引做你想要的条件,看看你的更新小提琴

JS : JS:

 $('#my-table').on('click', 'tbody td', function () {

     if($(this).index() < 4){ //click in td  between 1 and 4
         alert('td between 1 and 4 clicked');
     }else{ //click in another td
         alert('td between 5 and 6 clicked');   
     }

 });

Hope that help. 希望有所帮助。

It's important to understand, that the code like $('#my-table').on('click', 'tbody td:not(:first-child)', function (e) {...}); 重要的是要理解代码,如$('#my-table').on('click', 'tbody td:not(:first-child)', function (e) {...}); creates first jQuery wrapper with all <td> element which corresponds 'tbody td:not(:first-child)' selector and then bind the event handler separately to every from DOM elements in jQuery object. 使用所有<td>元素创建第一个jQuery包装器,该元素对应'tbody td:not(:first-child)'选择器,然后将事件处理程序分别绑定 jQuery对象中的每个 DOM元素。

I would recommend you to choose another way. 我建议你选择另一种方式。 You can make one binding of click on the whole <table> . 您可以对整个<table>进行一次 click绑定。 The event bubbling will forward the click on the cell to the parent <tr> and later to the <table> . 事件冒泡将单元格上的单击转发到父<tr> ,然后转发到<table> It's important that e.target get your the clicked <td> . e.target获取点击的<td>非常重要。

So the code could be the following: 所以代码可能如下:

var columnIndexesIgnore = [0, 3];
$('#my-table').on('click', function (e) {
    var $td = $(e.target).closest("td"); // e.target can be <span> instead of <td>
    if ($td.length > 0 && $.inArray($td[0].cellIndex, columnIndexesIgnore) < 0) {
        // cellIndex is 0-based index. We display in alert 1-based column index
        alert("I've been clicked on column " + ($td[0].cellIndex + 1) + "!");
    }
});

I used cellIndex property of DOM of <td> . 我使用了<td>的DOM的cellIndex属性。 It's 0-based index of column of the <td> element. 它是<td>元素的列的从0开始的索引。 So you need ignore clicks if $td[0].cellIndex is 0 or 3. 因此,如果$td[0].cellIndex为0或3,则需要忽略点击次数。

See your demo after the modification: http://jsfiddle.net/OlegKi/spckrjvf/5/ 修改后看你的演示: http//jsfiddle.net/OlegKi/spckrjvf/5/

You can check the desired condition by doing this. 您可以通过执行此操作来检查所需的条件。

$('td').click(function () {
    var col = $(this).parent().children().index($(this));
    var row = $(this).parent().parent().children().index($(this).parent());
    if (col == 3 || col == 0) {
        alert("I have clicked on column " + col);
    } else {
        alert("I have clicked on another column");
    }
});

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

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