单击时添加类,再次单击该特定tr或span时删除

时间:2014-02-28 18:18:34

标签: javascript jquery html

当我点击任何它选择表中的所有行但我想特定于点击。

我的js是 -

$(document).ready(function() {
    //appendScroll(scrollbarElement = $('.acraft-panelgroup'))
    $("#runway-select > tr").click(function() {
        // $("#runway-select ").parent('td span').removeClass("activehero");
        $("#runway-select span").parent().toggleClass("activehero");
    });
})

我的HTML是 -

<tbody id="runway-select">
    <tr class="text-center">
        <td><span class="glyphicon glyphicon-ok"></span>  </td>
        <td>04</td>
        <td>7102<span class="glyphicon glyphicon-info-sign"></span></td>
        <td><input type="text" class="form-control" /></td>
        <td></td>
</tbody>

如果可以,请帮帮我。我尝试使用Parent()但它不起作用。

2 个答案:

答案 0 :(得分:0)

 $(this).toggleClass("activehero");

这将切换tr上的课程。

 $(this).find('span').toggleClass("activehero");

这将在span内的每个tr上切换课程。

事件处理程序中的

this将是当前目标。它是您单击的HTML元素。

.find()搜索从指定目标降序的指定元素。

答案 1 :(得分:0)

我假设您要根据点击的行切换表格列的activehero个字段,如果是,那么您可以使用:

$("#runway-select > tr").click(function(){ 
    $(this).find('td').toggleClass("activehero");
});
相关问题