Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何查找HTML5';数据';使用Javascript解析xml文件时的属性?_Javascript_Xml_Html - Fatal编程技术网

如何查找HTML5';数据';使用Javascript解析xml文件时的属性?

如何查找HTML5';数据';使用Javascript解析xml文件时的属性?,javascript,xml,html,Javascript,Xml,Html,这是我的密码。我正试图拉出“data”属性,并将其放入id为“question”的末尾的div中。我对JS还很陌生,我知道一定有一些基本的东西我做错了,这是什么 <!DOCTYPE HTML> <html> <head> <script language="javascript" type="text/javascript"> function display() { var base = document.getElementB

这是我的密码。我正试图拉出“data”属性,并将其放入id为“question”的末尾的div中。我对JS还很陌生,我知道一定有一些基本的东西我做错了,这是什么

<!DOCTYPE HTML>

<html>
 <head>
  <script language="javascript" type="text/javascript">

 function display() {

  var base = document.getElementById("output");
  var CompleteSuggestion = base.getElementsByTagName("CompleteSuggestion")[1];
  var suggest = CompleteSuggestion.getElementsByTagName("suggest")[0];
  var question = suggest.getAttribute("data")[0].firstChild.data;



  document.getAttribute("data").innerHTML = question;


 }


  </script>

 </head>
 <body onload="display()">

 <xml id="output" style="display: none;">

        <CompleteSuggestion>
        <suggest data="hello">
      </CompleteSuggestion> 


 </xml>

<div class="question" id="question"></div>

 </body>
</html>

函数显示(){
var base=document.getElementById(“输出”);
var CompleteSuggestion=base.getElementsByTagName(“CompleteSuggestion”)[1];
var suggest=completessuggestion.getElementsByTagName(“建议”)[0];
var question=suggest.getAttribute(“数据”)[0].firstChild.data;
document.getAttribute(“数据”).innerHTML=问题;
}

以下是从xml文件获取属性的更好方法

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script>

//use ajax to grab the xml file
//I had an xml file named your.xml in the same directory as this script
$.ajax({
    type: "GET",
    url: "your.xml",
    dataType: "xml",
    success: function (xml) {

        //when the file has loaded via the ajax request loop over each suggestion element
        $(xml).find('suggestion').each(function() {

            //get the value of the first attribute (data), from this suggestion row
            console.log( this.attributes[0].value );
        });
    }
});
</script>

//使用ajax获取xml文件
//我有一个名为your.xml的xml文件,它与此脚本位于同一目录中
$.ajax({
键入:“获取”,
url:“your.xml”,
数据类型:“xml”,
成功:函数(xml){
//当文件通过ajax请求循环加载到每个建议元素上时
$(xml).find('suggestion').each(function(){
//从该建议行获取第一个属性(数据)的值
console.log(this.attributes[0].value);
});
}
});

对于提供的xml,我相信
CompleteSuggestion
空的
,因为您使用了索引
[1]
。你的意思是索引
[0]