这是我的代码。我试图提取数据"使用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>
答案 0 :(得分:0)
这是从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>