0

使用以下脚本时出现上述错误:

 <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'的值:对象为空或未定义

上面的例子我指的是这个网站。

http://www.javascriptkit.com/dhtmltutors/ajaxgetpost3.shtml

提前致谢。

4

1 回答 1

1

此脚本正在尝试将 添加output到您页面中的元素id="result"

因此,在您使用此脚本的页面的 html 中,您应该有一个带有id“结果”的元素。

例如,在您的页面上,您应该有类似的内容:

<div id="result"></div>

并且document.getElementById("result").innerHTML = output会将所有内容添加innerHTML到该div。

希望这是有道理的,如果不是,那么只需添加评论,我会尝试澄清/帮助更多。

于 2013-04-26T13:10:23.327 回答