开发者

Display or hide an XML tag within a table

开发者 https://www.devze.com 2023-04-09 14:35 出处:网络
I am trying to do a basic if/else statement to display or hide content within a table. It\'s also important to note that this content is being fed from an XML document. I am looking for an XML tag cal

I am trying to do a basic if/else statement to display or hide content within a table. It's also important to note that this content is being fed from an XML document. I am looking for an XML tag called .

I have the following code and cannot figure out how to make this work. Whatever I try, it displays nothing on the page. My logic seems right, but I'm also not a great script writer; so any guidance would be greatly appreciated. Thank you for your time and help.

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';
    }
}

....or what if I toggle the visibility of a div on and off?:

if (none != NONE){
document.write("<div id='test' style='background-color: #999;'>")
    document.write(y[j].getElementsByTag开发者_如何学GoName("note")[0].childNodes[0].nodeValue);
document.write("</div>");
}else{
    document.getElementById('test').style.display = 'none';
}


Below i give a sample example.book.xml page is written below.you can check it.

    <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>


For those interested, my condition was incorrect. It should have been something like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号