繁体   English   中英

使用Jquery查找表的父表,td的父表

[英]Use Jquery to find the parent table, of a table, of a td

我正在自定义Sage CRM,因此无法控制所编写的HTML,并且无法向CRM发出的表布局添加ID或类。 我想根据用户对选择下拉列表的选择来隐藏更高级别(而不是顶层)的表。 我只能将jQuery选择器连接到要隐藏的表中的表的标题行上。

DOM类似于:

//Lots of other table structures above this in the DOM....
<table>  <---- this is the table I want to show or hide based on the users selection
  <tbody>
    <tr>
     <td>
       <table>
         <tbody>
           <tr>
             <td class="PANEREPEAT">  <---- this is the node I can get selector to
                 Valuation information
////

所以我做下面的客户端JavaScript:

    var val_information_screen;

    $('.PANEREPEAT').filter(function () {
        //Find the valuation information screen
        return $(this).text() == 'Valuation information';
    }).each(function () { //iterate through all of these (there should only be one!)
        val_information_screen = $(this);
    });

    var sel_ofee_type = $('#ofee_type');
    if (sel_ofee_type.val() == '006') {
        val_information_screen.closest('table').parents("table:first").show();
    } else {
        val_information_screen.closest('table').parents("table:first").hide();
    }

它确实有效,但并不是特别漂亮。 我真正讨厌的是下面。 有没有更好的方法使用jQuery遍历DOM?

val_information_screen.closest('table').parents("table:first").show();
val_information_screen.closest('table').parents("table:first").hide();

如果您确定它具有固定的结构,则可以使用它,

$(td-selector).parents("table").eq(1).hide();

就你而言

val_information_screen.parents("table").eq(1).hide();

如果您的DOM(特别是从要隐藏的表开始,直到要作为选择器的td)非常固定,则可以使用以下选择器。

$('#element').parents('table').eq(1)

暂无
暂无

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

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