显示或隐藏表中的 XML 标记

发布于 2024-12-07 16:35:14 字数 960 浏览 0 评论 0原文

我正在尝试执行基本的 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';
}

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].getElementsByTagName("note")[0].childNodes[0].nodeValue);
document.write("</div>");
}else{
    document.getElementById('test').style.display = 'none';
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远昼 2024-12-14 16:35:14

下面我给出了一个示例,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>

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>
余生共白头 2024-12-14 16:35:14

对于那些感兴趣的人,我的条件是不正确的。本来应该是这样的:

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

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

(y[j].getElementsByTagName("note")[0].childNodes[0].nodeValue != "NONE")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文