If i have the following XML:
如果我有以下XML:
<Books>
<Book>
<Name>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?
如何使用jQuery的名称值等于“test”的子元素选择Book元素?
4 个解决方案
#1
9
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:
其中$ xml是表示XML文档的jQuery对象。我假设您通过Ajax加载XML文档。在这种情况下,你可以像这样构造一个这样的jQuery对象:
var $xml = $( data );
where data
is the Ajax-response.
数据是Ajax响应的地方。
#2
4
$('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".)
(这将匹配“测试”以及“数学测试”和“这只是一个测试”。)
#3
4
$("Books Book Name:Contains('Test')");
#4
0
To avoid selecting Name
element, use the .has()
filter function like so:
要避免选择Name元素,请使用.has()过滤器函数,如下所示:
$('Books Book').has('Name:contains("Test")')
This will return the Book
items with the name of "Test".
这将返回名为“Test”的Book项目。
#1
9
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:
其中$ xml是表示XML文档的jQuery对象。我假设您通过Ajax加载XML文档。在这种情况下,你可以像这样构造一个这样的jQuery对象:
var $xml = $( data );
where data
is the Ajax-response.
数据是Ajax响应的地方。
#2
4
$('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".)
(这将匹配“测试”以及“数学测试”和“这只是一个测试”。)
#3
4
$("Books Book Name:Contains('Test')");
#4
0
To avoid selecting Name
element, use the .has()
filter function like so:
要避免选择Name元素,请使用.has()过滤器函数,如下所示:
$('Books Book').has('Name:contains("Test")')
This will return the Book
items with the name of "Test".
这将返回名为“Test”的Book项目。