Javascript/XMLDOM不工作?
我在使用XML DOM时遇到了问题。(以前从未真正使用过它,但我想我应该试一试。)-我在我的网站上使用它来制作我的作品集(以及其他包含内容的页面),将缩略图链接到它们的链接、标题、描述等 不管怎样,我不太确定问题出在哪里,但它不起作用 .jsJavascript/XMLDOM不工作?,javascript,xml,Javascript,Xml,我在使用XML DOM时遇到了问题。(以前从未真正使用过它,但我想我应该试一试。)-我在我的网站上使用它来制作我的作品集(以及其他包含内容的页面),将缩略图链接到它们的链接、标题、描述等 不管怎样,我不太确定问题出在哪里,但它不起作用 .js 函数加载xmldoc(dname) { if(window.XMLHttpRequest) { xhttp=newXMLHttpRequest(); } 其他的 { xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”); }
函数加载xmldoc(dname)
{
if(window.XMLHttpRequest)
{
xhttp=newXMLHttpRequest();
}
其他的
{
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”、dname、false);
xhttp.send();
返回xhttp.responseXML;
}
函数imageList(值)
{
xmlDoc=loadXMLDoc(“uploads.xml”);
x=xmlDoc.getElementsByTagName(值)[0].childNodes;
对于(i=0;i,看起来你只是没有处理。从你的代码在JSFIDLE中制作一个完美的演示有点棘手(JSFIDLE不允许document.write
,它的AJAX echo是一个后期服务,但是有了一点jQuery,你可以隐藏这些细节(是的,我知道我对jQuery做了这样的批评)并重点对呈现代码()进行以下简单更改:
首先添加这两个辅助函数(从):
成功回调可以重写为:
function imageList(value)
{
xmlDoc=loadXMLDoc("uploads.xml");
x=xmlDoc.getElementsByTagName(value)[0].childNodes;
for (i=0;i<x.length;i++)
{
if(!is_ignorable(x[i])) {
document.write("<h1>"+x[i].getAttribute('id')+"</h1>");
document.write("<ul style='list-style-type: none;'>");
y=x[i].childNodes;
for(j=0;j<y.length;j++)
{
if(!is_ignorable(y[j])) {
document.write("<li style='background: url("+y[j].getAttribute('thumbnail')+") no-repeat center center;'><a href='#'></a></li>");
}
}
document.write("</ul>");
}
}
}
函数图像列表(值)
{
xmlDoc=loadXMLDoc(“uploads.xml”);
x=xmlDoc.getElementsByTagName(值)[0].childNodes;
对于(i=0;我可能复制的不起作用的内容?非常感谢!(:工作得很好。
<html>
<head>
<script type="text/javascript" src="js/xmldata.js"></script>
</head>
<body>
<script>
imageList("portfolio");
</script>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<portfolio>
<year id="2014">
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
</year>
<year id="2013">
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
</year>
<year id="2012">
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
</year>
<year id="2011">
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
<image thumbnail="" href="" desc=""></image>
</year>
</portfolio>
function is_ignorable( nod )
{
return ( nod.nodeType == 8) || // A comment node
( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
}
function is_all_ws( nod )
{
// Use ECMA-262 Edition 3 String and RegExp features
return !(/[^\t\n\r ]/.test(nod.data));
}
function imageList(value)
{
xmlDoc=loadXMLDoc("uploads.xml");
x=xmlDoc.getElementsByTagName(value)[0].childNodes;
for (i=0;i<x.length;i++)
{
if(!is_ignorable(x[i])) {
document.write("<h1>"+x[i].getAttribute('id')+"</h1>");
document.write("<ul style='list-style-type: none;'>");
y=x[i].childNodes;
for(j=0;j<y.length;j++)
{
if(!is_ignorable(y[j])) {
document.write("<li style='background: url("+y[j].getAttribute('thumbnail')+") no-repeat center center;'><a href='#'></a></li>");
}
}
document.write("</ul>");
}
}
}