Jquery query XML document where clause

北战南征 提交于 2019-12-23 02:07:34

问题


I have an XML document that I want to search a specific date and get information for just that date. My XML looks like this:

    <month id="01">
        <day id="1">
            <eitem type="dayinfo">
                <caption>
                    <text lang="cy">f. 3 r.</text>
                    <text lang="en">f. 3 r.</text>
                </caption>
                <ref href="link" id="3"/>
                <thumb href="link" id="3"/>
            </eitem>
        </day>
        <day id="7">
            <eitem type="dayinfo">
                <caption>
                    <text lang="cy">f. 5 v.</text>
                    <text lang="en">f. 5 v.</text>
                </caption>
                <ref href="link" id="4"/>
                <thumb href="link" id="4"/>
            </eitem>
        </day>
        <day id="28">
            <eitem type="dayinfo2">
                <caption id="1">
                    <text lang="cy">test</text>
                    <text lang="en">test2</text>
                </caption>
                <ref href="link" id="1"/>
                <thumb href="link" id="1"/>
            </eitem>
        </day>
        <day id="28">
            <eitem type="dayinfo">
                <caption>
                    <text lang="cy">f. 14 v.</text>
                    <text lang="en">f. 14 v.</text>
                </caption>
                <ref href="link" id="20"/>
                <thumb href="link" id="20"/>
            </eitem>
        </day>
    </month>

My Jquery looks like this:

            $(xml).find('month[id=01]').each(function()
            {
                $(xml).find("day").each(function()
                {
                    var day = $(this).attr('id');
                    alert(day);
                });
            });

In the XML example above I only shown one month, however there are many more. In my JQuery I've tried to do 'Where month = 1' and get all the days info for that month, however that JQuery brings back days for every month. How do I do a where clause with JQuery/JavaScript on a XML document? thanks.


回答1:


   $(xml).find('month[id=01]').each(function()
        {
            $(this).find("day").each(function()
          //  ^^^^
            {
                var day = $(this).attr('id');
                alert(day);
            });
        });


来源:https://stackoverflow.com/questions/4712311/jquery-query-xml-document-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!