使用Jquery查找td的表的父表

Mag*_*air 2 javascript jquery crm sage-crm

我正在定制Sage CRM,所以我无法控制所写的HTML,也无法将ID或类添加到CRM吐出的表格布局中.我想根据用户选择的选择下拉列表隐藏更高(不是顶级)级别的表.我只能将一个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
////
Run Code Online (Sandbox Code Playgroud)

所以我做了以下客户端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();
    }
Run Code Online (Sandbox Code Playgroud)

它确实有效,它不是特别漂亮.我真的讨厌的一点是在下面.有没有更好的方法来使用jQuery遍历DOM?

val_information_screen.closest('table').parents("table:first").show();
val_information_screen.closest('table').parents("table:first").hide();
Run Code Online (Sandbox Code Playgroud)

Opt*_*ime 6

如果你确定它有固定的结构,那么你可以使用它,

$(td-selector).parents("table").eq(1).hide();
Run Code Online (Sandbox Code Playgroud)

在你的情况下,

val_information_screen.parents("table").eq(1).hide();
Run Code Online (Sandbox Code Playgroud)