javascript - 如何选择包含 <a> 的 <tr> 并更改颜色

标签 javascript jquery

我有一个表结构:

<table style="width: 100%;">
            <tr>
                <td>
                  <a href="#" class="xx">one</a>
                </td>
                <td>Two</td>
                <td>Three</td>

            </tr>
            <tr>
                <td>
                  <a href="#" class="xx">Four</a>
                </td>
                <td>Five</td>
                <td>Six</td>

            </tr>
            <tr>
                <td>
                  <a href="#" class="xx">Seven</a>
                </td>
               <td>Eight</td>
                <td>Nine</td>

            </tr>
        </table>

CSS:

 .yy
    {
     background-color: red;    
    }

现在的问题是如果我点击 <a>其对应的<tr>元素( <tr> 持有 <a> )背景应该是红色的,如果我再次点击相同的 <a>它应该恢复正常。 如何识别<tr> <a>被点击。

编辑: 我试过: $(".yy").not($(this).parent().parent()).removeClass("yy"); $(this).parent().parent().toggleClass("yy"); 它奏效了。

最佳答案

$('a').live('click', function(){

$(this).parent().parent() //this is how you select the <tr> that the <a> is in

//first .parent() gets the <td> second .parent() gets the <tr>

});

如果您需要更多,请告诉我。可能有更好的方法,但我并不乐观。

关于javascript - 如何选择包含 <a> 的 <tr> 并更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3593598/

相关文章:

javascript - 为什么我不能用 Javascript 写 [1,2,3].reduce(Math.max)?

javascript - 如何将jquery的这个对象传递到内部成功函数中?

javascript - "Invalid subject"尝试使用 Cypress 断言 CSS 属性时

jquery - 将 HTMl5 应用程序移动到 ipad 时要注意什么?

jquery - 如何处理书签和绝对定位的div

javascript - 有没有更好的方法来编写这些函数?

javascript - AJAX 调用期间 UI 无响应

javascript - 禁用提交按钮,同时仍允许 PHP 脚本在提交表单时运行

jquery - 修复在 Bootstrap 选项卡上无法正常工作的问题

javascript - 经管理员批准后更新数据库