douyao1994 2016-07-11 01:55 采纳率: 100%
浏览 21
已采纳

Jquery删除不在表行上工作的类

I posted another question for issue and it was partially solved. I have a table with a single link on each row. The link calls AJAX and outputs in another Bootstrap column. This works fine. When I click the link, the class "highlight" is applied which changes the background color of the row. When I click another link I want the previously clicked clink to have the "highlight" class removed and the new row to have the class applied.

Currently, the add class works but any other link click get the class added but the class is never removed.

<table class="table table-hover unpadded" id="mashed_tab">
                    <thead>
                        <tr>
                            <th class="artcl_hdr text-center" colspan="2">
                                <p class="big_date top_pad"><i class="fa fa-rss fa-lg concrete"></i>&nbsp; Articles/Keywords for
                                2016-07-09 - 2016-07-10</p>
                            </th>
                            <th><span class="badge bkgcol-wet-asphalt">55</span></th>
                        </tr>
                    </thead>
                    <tbody class="mashed_body">
                        <tr class="mashed_row">
                            <td class="bkgcol-sunflower wht-border">
                                <div class="date_cell">
                                    <span class="asphalt long_date">Sat</span><span class="white day">Jul&nbsp;09,&nbsp;2016</span>
                                </div>
                            </td>
                            <td class="linked-title dark unpadded">
                                <div class="cell_link">
                                    <a href="javascript:;" id="linksplode" link_id="425">link 1</a>
                                </div>
                            </td>
                            <td class="small-cell">
                                <i class="fa fa-arrow-right"></i>
                            </td></tr><tr class="mashed_row">
                            <td class="bkgcol-sunflower wht-border">
                                <div class="date_cell">
                                    <span class="asphalt long_date">Sat</span>
                                    <span class="white day">Jul&nbsp;09,&nbsp;2016</span>
                                </div>
                            </td>
                            <td class="linked-title dark unpadded">
                                <div class="cell_link">
                                    <a href="javascript:;" id="linksplode" link_id="405">Link 2</a>
                                </div>
                            </td>
                            <td class="small-cell">
                                <i class="fa fa-arrow-right"></i>
                            </td>
                        </tr>
                        <tr class="mashed_row">
                            <td class="bkgcol-sunflower wht-border">
                                <div class="date_cell">
                                    <span class="asphalt long_date">Sat</span>
                                    <span class="white day">Jul&nbsp;09,&nbsp;2016</span>
                                </div>
                            </td>
                            <td class="linked-title dark unpadded">
                                <div class="cell_link">
                                    <a href="javascript:;" id="linksplode" link_id="426">Link 3</a>
                                </div>
                            </td>
                            <td class="small-cell">
                                <i class="fa fa-arrow-right"></i>
                            </td>
                        </tr>
                    </tbody>
                </table>


        <script type="text/javascript">
        $(document).ready(function() {
            attachEvents();
         });

         function attachEvents() {
            var selClass = 'highlight';

           $('#mashed_tab tr a').click(function() {
           $(this).parent().parent().removeClass(selClass);
           $(this).parent().parent().addClass(selClass);
          }); 
         };
    </script>
  • 写回答

1条回答 默认 最新

  • dongshi1606 2016-07-11 02:41
    关注

    Try something like this in your on click function and let me know.

    $('tr a').click(function() { 
    
    $("#mashed_tab tr").find("." + selClass).removeClass(selClass); 
    
    $(this).parent().parent().addClass(selClass); });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题