简体   繁体   English

单击该行时将选定的类添加到表行

[英]Add selected class to table row when click on that row

i have table 我有桌子

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="activity_tab">
    <tr id="list_1">
        <td><span class="new_act">Folder files Created</span>
        </td>
        <td>2013-06-24 02:11 am</td>
    </tr>
    <tr id="list_2">
        <td><span class="new_act">Folder ss Created</span>
        </td>`enter code here`
        <td>2013-06-24 02:11 am</td>
    </tr>

and its dynamic table data comes on run time when i click on a row i want to add selected class to that row. 当我单击一行时,要向其添加选定的类,它的动态表数据就会在运行时出现。 after that i have hyper link at top of page 之后,我在页面顶部有超级链接

 <li><a href="javascript:void(0);" class="delete">Delete</a></li>

this is my hyperlink now when click on hyperlink the selected row will bhe deleted how can i get the selected row and i have all idea about its deletion but could'nt find any solution... 这是我现在的超链接,当单击超链接时,选定的行将被删除,我如何获得选定的行,我对删除它有全部了解,但找不到任何解决方案...

You can do this - 你可以这样做 -

$('.activity_tab tr').on('click',function(){
  $(this).addClass('selected');
});
$('.delete').on('click',function(){
   $('tr.selected').remove();
});

I would add a class on the selected row. 我将在所选行上添加一个类。 I'm assuming you can only have one selected row at a time, so you'll need to remove any selections when a new row is clicked. 我假设您一次只能选择一个行,因此单击新行时需要删除所有选择。

Since your table is dynamic, you'll need to use a delegated event listener to add the selected class. 由于表是动态的,因此您需要使用委托事件侦听器来添加所选的类。 See the documentation of jQuery's on() method for more details on delegated events. 有关委托事件的更多详细信息,请参见jQuery on()方法的文档。

$(document).on('click', 'tr', function(e) {
    $('tr.selected').removeClass('selected'); //clear other selections
    $(this).addClass('selected'); //mark this row as selected
});

$('a.delete').on('click', function(e){
   e.preventDefault(); //stop native link behavior
   $('tr.selected').remove(); //remove selected row
});

Note that the listener that adds the selected class to a row will affect any tr element in the document. 请注意,将selected类添加到行的侦听器将影响文档中的任何tr元素。 If you have other tables on your page, you will need to make the second selector (after the 'click') more specific. 如果页面上还有其他表格,则需要使第二个选择器(在“单击”之后)更加具体。

You can try this 你可以试试这个

$(function(){
    $(".activity_tab tr").click(function(){
        $(this).addClass("red");
    });

    $('.delete').on('click',function(){
       $('tr.red').remove();
    });
});

Demo: http://jsfiddle.net/YNNVr/ 演示: http//jsfiddle.net/YNNVr/

You can add selected class to tr on click with following code. 您可以使用以下代码在点击时将selected类添加到tr

$('.activity_tab').on('click', 'tr', function(){
   $(this).addClass('selected');
});

And if you want to take that one step further, you can use toggleClass instead of addClass , so you can remove the selected class with just another click on the tr . 而且,如果您想更进一步,可以使用toggleClass而不是addClass ,因此只需单击tr即可删除selected类。

You can then select all the selected rows and delete them 然后,您可以选择所有selected行并将其删除

$('.delete').on('click', function(){
   $('activity_tab .selected').remove();
});

This removes all rows with selected class in the table with activity_tab class. 这将删除带有activity_tab类的表中具有selected类的所有行。

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

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