在表中显示或隐藏XML标记

时间:2011-10-01 12:06:37

标签: javascript xml if-statement

我正在尝试使用基本的if / else语句来显示或隐藏表中的内容。同样重要的是要注意这些内容是从XML文档中提供的。我正在寻找一个名为。

的XML标签

我有以下代码,无法弄清楚如何使这项工作。无论我尝试什么,它都不会在页面上显示任何内容。我的逻辑似乎是正确的,但我也不是一个伟大的剧本作家;所以任何指导都将非常感谢。感谢您的时间和帮助。

n = xmlDoc.getElementsByTagName("note")[0].childNodes[0].nodeValue

if (n != NONE){
        document.write("<tr>");
    document.write("<td colspan='4' id='notation'>");
    document.write(y[j].getElementsByTagName("note")[0].childNodes[0].nodeValue);
    document.write("</td>");
    document.write("</tr>");
}else{
    document.getElementsByTagName("note").style.display = 'none';
    }
}

....或者如果我打开和关闭div的可见性怎么办?:

if (none != NONE){
document.write("<div id='test' style='background-color: #999;'>")
    document.write(y[j].getElementsByTagName("note")[0].childNodes[0].nodeValue);
document.write("</div>");
}else{
    document.getElementById('test').style.display = 'none';
}

2 个答案:

答案 0 :(得分:0)

下面我给出一个示例example.book.xml页面,如下所示。您可以查看它。

    <script>
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","book.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 
n = xmlDoc.getElementsByTagName("dow")[0].childNodes[0].nodeValue;
var y=xmlDoc.getElementsByTagName("canceledDate");
for (j=0;j<y.length;j++){
if (n){
        document.write("<tr>");
    document.write("<td colspan='4' id='notation'>");
    document.write(y[j].getElementsByTagName("dow")[0].childNodes[0].nodeValue);
    document.write("</td>");
    document.write("</tr>");
}else{
    document.getElementsByTagName("dow").style.display = 'none';

}
    </script>


<cancellations>
<canceledDate>
    <dow>Tuesday</dow>
    <month>10</month>
    <day>07</day>
    <year>11</year>
    <canceledClass>
    <title>title</title>
    <course>course</course>
    <section>section</section>
    <days>days</days>
    <instructor>Doe</instructor>
    <note>NONE</note>
    </canceledClass>
</canceledDate>
<canceledDate>
    <dow>Wednesday</dow>
    <month>10</month>
    <day>07</day>
    <year>11</year>
    <canceledClass>
    <title>title</title>
    <course>course</course>
    <section>section</section>
    <days>days</days>
    <instructor>Doe</instructor>
    <note>this is a note</note>
    </canceledClass>
</canceledDate>
</cancellations>

答案 1 :(得分:0)

对于那些感兴趣的人,我的情况不正确。应该是这样的:

(y[j].getElementsByTagName("note")[0].childNodes[0].nodeValue != "NONE")