使用下面的脚本时出现上述错误:
<script type="text/javascript">
function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}
var mygetrequest=new ajaxRequest()
if (mygetrequest.overrideMimeType)
mygetrequest.overrideMimeType('text/xml')
mygetrequest.onreadystatechange=function(){
alert("hi22222222");
if (mygetrequest.readyState==4){
if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
var xmldata=mygetrequest.responseXML //retrieve result as an XML object
alert("hi22222222 :" +xmldata);
var rssentries=xmldata.getElementsByTagName("item")
var output='<ul>'
for (var i=0; i<rssentries.length; i++){
output+='<li>'
output+='<a href="'+rssentries[i].getElementsByTagName('link')[0].firstChild.nodeValue+'">'
output+=rssentries[i].getElementsByTagName('title')[0].firstChild.nodeValue+'</a>'
output+='</li>'
}
output+='</ul>'
document.getElementById("result").innerHTML=output
}
else{
alert("An error has occured making the request")
}
}
}
mygetrequest.open("GET", "C:/HTML/JQueryExamples/javascriptkit.xml", true)
mygetrequest.send(null)
</script>
我使用xml来检索上面出错的数据。
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="0.91">
<channel>
<title>JavaScriptKit.com</title>
<link>http://www.javascriptkit.com</link>
<description>JavaScript tutorials and over 400+ free scripts!</description>
<language>en</language>
<item>
<title>Document Text Resizer</title>
<link>http://www.javascriptkit.com/script/script2/doctextresizer.shtml</link>
<description>This script adds the ability for your users to toggle your webpage's font size, with persistent cookies then used to remember the setting</description>
</item>
<item>
<title>JavaScript Reference- Keyboard/ Mouse Buttons Events</title>
<link>http://www.javascriptkit.com/jsref/eventkeyboardmouse.shtml</link>
<description>The latest update to our JS Reference takes a hard look at keyboard and mouse button events in JavaScript, including the unicode value of each key.</description>
</item>
<item>
<title>Dynamically loading an external JavaScript or CSS file</title>
<link>http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml</link>
<description>External JavaScript or CSS files do not always have to be synchronously loaded as part of the page, but dynamically as well. In this tutorial, see how.</description>
</item>
</channel>
</rss>
请帮我解决此错误。应该读取xml节点。
错误:无法设置属性innerHTML'的值:object为null或undefined
以上我在这个网站上引用的例子。
http://www.javascriptkit.com/dhtmltutors/ajaxgetpost3.shtml
提前感谢。
答案 0 :(得分:1)
此脚本正在尝试将output
添加到您网页中包含id="result"
的元素。
因此,在您使用此脚本的页面的html中,您应该有一个id
“结果”的元素。
e.g。在你的页面上你应该有:
<div id="result"></div>
document.getElementById("result").innerHTML = output
会将innerHTML
的所有内容添加到该div中。
希望这是有道理的,如果没有,那么只需添加评论,我会尝试澄清/帮助更多。