添加到getElementsByTagName的超链接

时间:2012-09-21 18:22:45

标签: html getelementsbytagname

我正在将XML节点值检索到我的HTML页面中。我的一个节点中有一条路径,我想在它周围放置一个链接。 TagName是(“link”)我需要添加TagName“link”的值以进行超链接。我试过用

这是怎么做到的?

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", "reports.xml", false);
                        xmlhttp.send();
                        xmlDoc = xmlhttp.responseXML;

                        //document.write("<tr><th width='18%' align='left'>Report</th>");
                        //document.write("<th width='18%' align='left'>Audit Subject</th>");
                        //document.write("<th width='18%' align='left'>Report</th></tr>");

                        var x = xmlDoc.getElementsByTagName("item");
                        for (i = 0; i < x.length; i++) {
                            document.write("<tr><td>");
                            document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
                            document.write("</td><td>");
                            document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
                            document.write("<br>");
                            document.write("<a href='' target='_blank'>");
                            document.write(x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);
                            document.write("</a></td><td>");
                            document.write(x[i].getElementsByTagName("pubdate")[0].childNodes[0].nodeValue);
                            document.write("</td></tr>");

                        }

XML文件:

`<audits>
  <reports>
    <title>Audit Reports</title>
    <rsslink>http://www.somesite.com/</rsslink>
    <item>
      <title>test1</title>
      <description>test for hyperlink path</description>
      <link>../pdf/audits/DeonGeeCert.pdf</link>
      <pubdate>Friday, September 21, 2012</pubdate>
    </item>
    <item>
      <title>test2</title>
      <description>test2</description>
      <link>../pdf/audits/2012-09-10audit-12-14.pdf</link>
      <pubdate>Monday, September 10, 2012</pubdate>
    </item>`

1 个答案:

答案 0 :(得分:1)

不确定你想要什么,你的问题太模糊了,但我猜这可能是你想做的事情

for (i = 0; i < x.length; i++) {
                            document.write("<tr><td>");
                            document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
                            document.write("</td><td>");
                            document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
                            document.write("<br>");
                            var linkValue=x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
                            document.write("<a href='"+linkValue+"' target='_blank'>");
                            document.write(linkValue);  //or some meaningful text, just keeping the last string in the link
                            document.write("</a></td><td>");
                            document.write(x[i].getElementsByTagName("pubdate")[0].childNodes[0].nodeValue);
                            document.write("</td></tr>");

                    }