如果data-id在数组中,则隐藏td

Jus*_*ind 0 html jquery

<table id="table1">
    <tr>
        <td class="rb-o" data-id="1">a</td>
        <td class="rb-o" data-id="2">b</td>
        <td class="rb-o" data-id="3">c</td>
    </tr>
    <tr>
        <td class="rb-o" data-id="4"></td>
        <td class="rb-o" data-id="5"></td>
        <td class="rb-o" data-id="6"></td>
    </tr>

</table>
Run Code Online (Sandbox Code Playgroud)

我想隐藏整个<td>但是只有当它全部<data-id>都在数组列表中时.该表是动态生成的.

我的jQuery:

myarray =["2", "4", "5"] ;

$('.rb-o').each(function(index) {
    if (myarray[index]) {
        $(this).attr('data-id', myarray[index]).show();
    }
    else{
        $(this).attr('data-id', myarray[index]).hide();
    }
Run Code Online (Sandbox Code Playgroud)

不能正常工作.

Aru*_*hny 6

尝试使用数组中的值创建属性过滤器,并使用它来过滤tds

myarray = ["2", "4", "5"];
$('.rb-o').filter($.map(myarray, function (val) {
    return '[data-id="' + val + '"]'
}).join()).hide()
Run Code Online (Sandbox Code Playgroud)

演示:小提琴