使用Jquery查找td的表的父表

时间:2013-08-06 16:28:39

标签: 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
////

所以我做了以下客户端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();

2 个答案:

答案 0 :(得分:6)

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

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

在你的情况下,

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

答案 1 :(得分:0)

如果您的DOM(特别是从您要隐藏的表开始直到您拥有选择器的td)几乎已修复,则可以使用以下选择器。

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