如何在 jQuery 中选择 xml 子节点?
现在,这段代码可以很好地解析 XML 文件,但是,在 XML 文件中,我有多个作者节点,我希望能够在每个作者之间添加逗号。 XML 的作者数量从一到四位不等。提前谢谢你了。
/* Load XML File */
$.ajax({
url: "xml/ajax-response-data.xml",
cache: false,
success: libraryXML
});
function libraryXML (xml) {
$(xml).find('book').each(function(){
/* Parse the XML File */
var id = $(this).attr('id');
var checked = $(this).attr('checked-out')
var title = $(this).find('title').text();
var isbn = $(this).find('isbn-10').text();
var authors = $(this).find('authors').text();
/* Spit out some books */
$('<li class="book-'+id+' checked'+checked+'"></li>').html('<span class="id">' + id + '</span><span class="title">' + title + '</span><span class="author">' + authors +'</span><span class="isbn">' + isbn + '</span>').appendTo('.library');
});
}
<book id="1" checked-out="1">
<authors>
<author>David Flanagan</author>
</authors>
<title>JavaScript: The Definitive Guide</title>
<isbn-10>0596101996</isbn-10>
</book>
<book id="2" checked-out="1">
<authors>
<author>John Resig</author>
</authors>
<title>Pro JavaScript Techniques (Pro)</title>
<isbn-10>1590597273</isbn-10>
</book>
<book id="3" checked-out="0">
<authors>
<author>Erich Gamma</author>
<author>Richard Helm</author>
<author>Ralph Johnson</author>
<author>John M. Vlissides</author>
</authors>
<title>Design Patterns: Elements of Reusable Object-Oriented Software</title>
<isbn-10>0201633612</isbn-10>
</book>
Right now this code parses the XML file fine, however, in the XML file I have multiple author nodes, I'd like to be able to put a comma in between each author. The XML varies from one to from one to four authors. Thank you ahead of time.
/* Load XML File */
$.ajax({
url: "xml/ajax-response-data.xml",
cache: false,
success: libraryXML
});
function libraryXML (xml) {
$(xml).find('book').each(function(){
/* Parse the XML File */
var id = $(this).attr('id');
var checked = $(this).attr('checked-out')
var title = $(this).find('title').text();
var isbn = $(this).find('isbn-10').text();
var authors = $(this).find('authors').text();
/* Spit out some books */
$('<li class="book-'+id+' checked'+checked+'"></li>').html('<span class="id">' + id + '</span><span class="title">' + title + '</span><span class="author">' + authors +'</span><span class="isbn">' + isbn + '</span>').appendTo('.library');
});
}
<book id="1" checked-out="1">
<authors>
<author>David Flanagan</author>
</authors>
<title>JavaScript: The Definitive Guide</title>
<isbn-10>0596101996</isbn-10>
</book>
<book id="2" checked-out="1">
<authors>
<author>John Resig</author>
</authors>
<title>Pro JavaScript Techniques (Pro)</title>
<isbn-10>1590597273</isbn-10>
</book>
<book id="3" checked-out="0">
<authors>
<author>Erich Gamma</author>
<author>Richard Helm</author>
<author>Ralph Johnson</author>
<author>John M. Vlissides</author>
</authors>
<title>Design Patterns: Elements of Reusable Object-Oriented Software</title>
<isbn-10>0201633612</isbn-10>
</book>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将您的代码更改为如下所示:
优点是它可以像您想要的那样以逗号分隔作者,但它还可以通过使用 jQuery 的文本函数 对输出进行 HTML 转义。
I'd change your code to something like this:
The advantages are that it does the comma-separated author, like you wanted, but it also prevents any XSS attacks in the generated HTML by using jQuery's text function to HTML-escape the output.