相交两个jQuery选项

时间:2014-07-15 14:28:15

标签: jquery

jQuery拥有ability to union the results of two selections,使用.add()

var foosAndBars = $(“。foo”)。add(“。bar”); //选择所有元素类.foo或.bar

但我需要执行两组交叉

var allTRs = $("#lvTransactions").find("[data-language]").closest("tr")
var trMatchingLanguage= allTRs.find("[data-language]").filter("[data-language='pascal'],[data-language='C#'],[data-langauge='Ook']").closest("tr");
var trMatchingTag = allTRs.find("data-tag").filter("[data-tag='wpf'").closest("tr");

我需要两个集合中共有的TR个元素,因为这些元素是要显示的元素:

allTRs.hide();    
trMatchingLanguage.intersect(trMatchingTag ).show();

修改jsFiddle

显示所有包含以下内容的行:

  • 数据语言pascalC#
  • 数据标记wpf

使用以下示例表:

<div id="lvTransactions">
    <table>
        <thead>
            <tr><th>Question</th><th>Lanaguage</th><th>Tag</th></tr>
        </thead>
        <tr>
            <td>How to</td>
            <td data-language="C">C</td>
            <td data-tag="wpf">Windows Presentation Foundation</td>
        </tr>
        <tr>
            <td>Why does</td>
            <td data-language="pascal">Pascal</td>
            <td data-tag="dwm">Windows Presentation Foundation</td>
        </tr>
        <tr>
            <td>Why can't</td>
            <td data-language="pascal">Pascal</td>
            <td data-tag="wpf">Desktop Window Manager</td>
        </tr>
        <tr>
            <td>Does it</td>
            <td data-language="C#">C#</td>
            <td data-tag="wpf">Desktop Window Manager</td>
        </tr>
        <tr>
            <td>Where is</td>
            <td data-language="Java">Java</td>
            <td data-tag="wpf">Windows Presentation Foundation</td>
        </tr>            
    </table>
</div>

现在我有(错误的)代码(显示联合,而不是交集):

//Only show rows that are both C#/Pascal and WPF

var allTRs = $("#lvTransactions").find("[data-language]").closest("tr")

var theLanguageTRs = allTRs.find("[data-language]").filter("[data-language='pascal'],[data-language='C#'],[data-langauge='ook']").closest("tr");
var theTagTRs = allTRs.find("[data-tag]").filter("[data-tag='wpf']").closest("tr");

1 个答案:

答案 0 :(得分:3)

如此简单的事情:

$(array1).filter(array2);

参考:$.filter()