HTML5包含Javascript document.write()不适用于XML文件

时间:2013-03-13 19:12:48

标签: javascript xml html5

我无法让Javascript在网站上列出“开始”数字,因为它已空。

这是我的html正文代码,

<script type="text/javascript">
    captionsDoc = loadXMLDoc("captions.xml");
    x=captionsDoc.getElementsByTagName('text');

    for(i=0;i<x.length;i++)
    {
        document.write(x[i].getAttribute('start'));
        document.write("/n");

    }
</script>

然后这是来自captions.xml

的xml代码示例
<transcript>
<text start="6.738" dur="2.277">and explain a few interesting points about them.</text>
<text start="9.016" dur="2.722">But first I need to crush your expectations.</text>
<text start="24.716" dur="1.611">So let&#39;s begin.</text>
<text start="26.328" dur="2.535">First we start with the solvent diethyl phthalate.</text></transcript>

1 个答案:

答案 0 :(得分:1)

你有

document.wite("
")
你应该

document.write("\n");

然后它在这里使用魅力,加载函数来自:http://www.w3schools.com/dom/dom_loadxmldoc.asp

就像这样:

<html>
    <head>
        <title>HTML5 included Javascript....</title>
        <meta name="description" content="Test" charset="utf-8"></meta>
        <script type="text/javascript">
            function loadXMLDoc(dname)
            {
                if (window.XMLHttpRequest)
                {
                xhttp=new XMLHttpRequest();
                }
                else
                {
                xhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xhttp.open("GET",dname,false);
                xhttp.send();

                return xhttp.responseXML;
            }

            function init()
            {
                var c = document.getElementById('container');
                captionsDoc = loadXMLDoc("captions.xml");
                x=captionsDoc.getElementsByTagName('text');

                for(i=0;i<x.length;i++)
                {
                    c.innerHTML += x[i].getAttribute('start');
                    c.innerHTML += "\n";
                }
            }

            window.onload = init;
        </script>
    </head>

    <body>
        <div id="container">
        </div>
    </body>
</html>