我有一段由 jQuery 生成的 XML。
我想要的是
<booklists>
<booklist>
<userid>0</userid>
<book>Foo</book>
<book>Bar</book>
</booklist>
<booklist>
<userid>1</userid>
<book>Foo</book>
<book>Bar</book>
</booklist>
<booklist>
<userid>2</userid>
<book>Foo</book>
<book>Bar</book>
</booklist>
<booklist>
<userid>3</userid>
<book>Foo</book>
<book>Bar</book>
</booklist>
<booklist>
<userid>4</userid>
<book>Foo</book>
<book>Bar</book>
</booklist>
</booklists>
我得到了什么
<booklists>
<booklist>
<userid>0</userid>
<book>Foo</book>
<book>Bar</book>
</booklist>
<booklist>
<userid>4</userid>
<userid>3</userid>
<userid>2</userid>
<userid>1</userid>
<book>Foo</book>
<book>Bar</book>
</booklist>
</booklists>
我的代码
lists = $('<booklists />')
books = $('<booklist />');
book = $('<book>Foo</book>');
books.append(book);
book = $('<book>Bar</book>');
books.append(book);
temp = books.clone();
for (var i = 0; i < 5; i++) {
books.prepend('<userid>' + i + '</userid>');
lists.append(books);
books = temp;
}
$('.output').html(lists);