我正在尝试使用javascript从XML文件加载数据,除链接外,它们都可以100%正常工作。如果我替换createDetails(i,"LINK")
一个字符串,它工作正常,除了我需要它来更改XML中的每个条目。但是使用createDetails(i,"LINK")
,链接是undefined.html,XML中的链接实例只是打印到一边。
代码:
预加载xml:
var x;
x=xmlDoc.getElementsByTagName("HOME");
function createDetails(refNum,tagName){
var tempProduct;
tempProduct = x[refNum].getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
document.write(tempProduct);
}
function createImage(refNum,tagName){
var tempProduct;
tempProduct = x[refNum].getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
return(tempProduct);
}
打印它的脚本(破碎的东西):
<script>
for(i = 0; i<x.length; i++){
/*this line doesn't work*/ document.write("<a href=\"" + createDetails(i,"LINK") + ".html\">");
document.write("<div class=\"homeLink\">");
document.write("<div class=\"homeLinkPic\">");
document.write("<img src=\"images/" + createImage(i,"PIC") + "\" width=\"200\" height=\"200\" alt=\"linkpic\" />");
document.write("</div>");
document.write("<div class=\"productDesc\">");
createDetails(i,"DESC");
document.write("</div>");
document.write("</div>");
document.write("</a>");
}
</script>
答案 0 :(得分:2)
createDetails
未返回值。将其更改为:
function createDetails(refNum,tagName){
var tempProduct;
tempProduct = x[refNum].getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
return tempProduct;
}
答案 1 :(得分:1)
您必须从createDetails
函数返回值。尝试
return tempProduct;
而不是
document.write(tempProduct);