开发者

Select element with a child element by value with jQuery

If i have the following XML:

<Books>
    <Book>
        <Name&开发者_JS百科gt;Test</Name>
        ...
    </Book>
    <Book>
        <Name>Another one</Name>
        ...
    </Book>
</Books>

How can I select the Book element with the child element whose name value equals "test" with jQuery?


var book = $xml.find( 'Name' ).filter(function () {
    return $( this ).text() === 'Test';
}).parent();

where $xml is the jQuery object that represents the XML document. I assume that you load the XML document via Ajax. In that case you can construct such a jQuery object like so:

var $xml = $( data );

where data is the Ajax-response.


$('Books Book Name:contains("Test")')

http://api.jquery.com/contains-selector/

(This will match "Test" as well as "Math Test", and "This Is Only A Test".)


$("Books Book Name:Contains('Test')");


To avoid selecting Name element, use the .has() filter function like so:

$('Books Book').has('Name:contains("Test")')

This will return the Book items with the name of "Test".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜