我有 Jquery 循环通过我的 XML 文件:
$(xml).find("category").each(function()
{
var title = $(this).find("title").text();
var i = 0;
console.log("j:" + j);
if (j > 0) {
fillNav("","",i,"final");
}
$(this).find('items > item').each(function() {
var text = $(this).find("text").text();
console.log("i:" + i);
fillNav(title,text,i,"text");
i++
});
j++;
});
它填充 div 的位置:
function fillNav(title,text,i,final){
console.log("i:" + i);
if ((i == 0)&&(final != "final")){
console.log("title:" + title + "div><p><ul>");
$("#accordionSub").append("<h3>" + title + "</h3><br /><div><p><ul>");
//$("#output").append("<div><p><ul>");
}
if (final != "final"){
console.log("<li" + "text:" + "/li>");
$("#accordionSub").append("<li>" + text + "</li>");
}
if ((i == 0) && (final == "final")){
console.log("final:" + "/ul>/p>/div>");
$("#accordionSub").append("</ul></p></div>");
}
}
结果html:
<h3>Accessories</h3><br><div><p></p><ul></ul></div><li>Adaptors, Bushings, and Pressure Snubbers</li><li>Cables and Accessories for pH Instruments</li><p></p>
为什么 UL 标签不在 LI 标签周围?
更新:
output += "<h3>" + title + "</h3><br />";
output += " <div><p><ul> " ;
$(this).find('items > item').each(function() {
var text = $(this).find("text").text();
var slink = $(this).find("link").text();
//console.log("i:" + i);
output += "<li class='subLink' src='"+ slink +"'>" + text + "</a></li>";
});
output += "</ul></p></div>";
产生:
<-div><-p></-/p><-ul><-li></-/li></-/ul><-p></-/p>
我必须让 P 标签包围 UL 标签
更新* 感谢所有帮助:只需发布我的最终代码以帮助将来的其他人:
$(xml).find("category").each(function()
{
var output += '<h3>' +$(this).find("title").text() + '</h3>' ;
var div = '<div>';
output += '<ul>';
$(this).find('items > item').each(function() {
var text = $(this).find("text").text();
var slink = $(this).find("link").text();
output += "<li class='subLink' src='"+ slink +"'>" + text + "</a></li>";
});
output += '</ul>';
});
//console.log(output);
var icons = {
header: "ui-icon-circle-arrow-e",
activeHeader: "ui-icon-circle-arrow-s"
};
$('<div>')
.attr('id','accordionSub')
.html(output)
.appendTo('#accordionSubB').delay(1).queue(function(){
$( "#accordionSub" ).accordion({
heightStyle: "content",
collapsible: true,
icons: icons
});
});