2013-04-20 67 views
1

我一直有這個代碼的麻煩。我盡力保留這本書,但這本書似乎是錯誤的。未捕獲的類型錯誤:無法設置爲null的屬性'innerHTML'

以下代碼應該顯示已選擇任何單選按鈕的RSS提要。您也可以在http://w3.cnm.edu/~bnoble/cis1210/afds/Travel/TravelDeals.html上查看網站。

在此先感謝您的幫助!

<html> 
<head> 
    <title>Travel Deals RSS Feed</title> 
    <script> 
     var rssRequest = false; 
     function getRequestObject() 
     { 
      try 
      { 
       rssRequest = new XMLHttpRequest(); 
      } 
      catch (requestError) 
      { 
       try 
       { 
       rssRequest = new ActiveXObject(); 
       } 
       catch (requestError) 
       { 
        try 
        { 
         rssRequest = new 
         ActiveXObject("Micorsoft.XMLHTTP"); 
        } 
        catch (requestError) 
        { 
         window.alert("I am sorry. Your browser does not support AJAX. Please upgrade your broweser and try again."); 
         return false; 
        } 
       } 
      } 
      return rssRequest; 
     } 
     function rssUpdate() 
     { 
      if(!rssRequest)rssRequest = getRequestObject(); 
      for (var i=0; i<6; ++i) 
      { 
       if (document.forms[0].deals[i].checked == true) 
       { 
        var rss = document.forms[0].deals[i].value; 
        break; 
       } 
      } 
      rssRequest.abort(); 
      rssRequest.open("get","TravelDeals.php?" + "rss" + rss, true); 
      rssRequest.send(null) 
      rssRequest.onreadystatechange=getRssDeals; 
      //clearTimeout(recentDeals); 
      //var recentDeals = setTimeout('rssUpdate()', 5000); 
     } 
     function getRssDeals() 
     { 
      if(rssRequest.readyState==4 && rssRequest.status == 200) 
      { 
       var deals = rssRequest.responseXML; 
       document.getElementById("rssFeed").innerHTML = "" 
       var dealsInfo=deals.getElementsByTagName("item"); 
       if (dealsInfo.length > 0) 
       { 
        for (var i=0; i<dealsInfo.length; ++i) 
        { 
         var curHeadline = dealsInfo[i].getElementsByTagName("title")[0].childNodes[0].nodeValue; 
         var curlink = dealsInfo[i].getElementsByTagName("link")[0].childNodes[0].nodeValue; 
         var curPubDate = dealsInfo[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue; 
         var curDesc = dealsInfo[i].getElementsByTagName("description")[0].childNodes[0].nodeValue; 
         var curDeal = "<a href='" + curLink + "'>" + curHeadline + "</a><br/>"; 
         curDeal += curDesc + "<br/>";curDeal += curDesc + "<br/>"; 
         document.getElementById("rssFeed").innerHTML += curDeal; 
        } 
       } 
       else 
        document.getElementById("rssFeed").innerHTML = "The RSS feed appears to be broken or empty."; 
      } 
     } 
    </script> 
</head> 
<body onload="rssUpdate()"> 
    <h1>Travel Deals RSS</h1> 
    <form method="get" action=""> 
     <table border="1"> 
      <colgroup span="1" width="125"/> 
      <colgroup span="1" width="350"/> 
      <tr> 
       <td align="top"> 
        <input type="radio" name="deals" value="http://www.orbitz.com/rss/topdeals-vacationpackages.rss.xml" onclick="rssUpdate()"/><Orbitz<br/> 
        <input type="radio" name="deals" value="http://www.travelocity.com/dealservice/globaltrips-shopping-svcs/deals-1.0/services/RssDealsServices?ProductType=Air&rdr=GEN&nm=My~Travelocity~Specials&typ=0&orig=ABQ&dest=PHX,LAS,LAX,NYC,LGB&id=9197I1366355523195" checked="checked" onclick="rssUpdate()"/><Travelocity<br/> 
        <input type="radio" name="deals" value="http://press.hotwire.com/index.php?s=43&pagetemplate=rss" onclick="rssUpdate()"/><Hotwire<br/> 
       </td> 
       <td id="rssFeed" valign="top></td> 
      </tr> 
     </table> 
    </form> 
</body> 

+0

我以前見過同樣的問題,併發布了一個答案,那個時候有一個ID爲rssFeed的'td'。 – 2013-04-20 02:28:34

+0

查看更新的解決方案 – 2013-04-20 03:37:36

回答

1

沒有與頁面ID rssFeed沒有元素。

您可以通過在頁面中加入了div ID爲rssFeed

<div id="rssFeed"></div> 

試試這個完整的代碼解決這個問題,因爲你在標記有未關閉的元素

<html> 
    <head> 
     <title>Travel Deals RSS Feed</title> 
     <script> 
      var rssRequest = false; 
      function parseXML(xml){ 
       var parser, xmlDoc; 
       if (window.DOMParser) { 
        parser = new DOMParser(); 
        xmlDoc = parser.parseFromString(xml,"text/xml"); 
       } else { 
        // Internet Explorer 
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
        xmlDoc.async = false; 
        xmlDoc.loadXML(xml); 
       } 
      } 
      function getRequestObject() 
      { 
       try 
       { 
        rssRequest = new XMLHttpRequest(); 
       } 
       catch (requestError) 
       { 
        try 
        { 
        rssRequest = new ActiveXObject(); 
        } 
        catch (requestError) 
        { 
         try 
         { 
          rssRequest = new 
          ActiveXObject("Micorsoft.XMLHTTP"); 
         } 
         catch (requestError) 
         { 
          window.alert("I am sorry. Your browser does not support AJAX. Please upgrade your broweser and try again."); 
          return false; 
         } 
        } 
       } 
       return rssRequest; 
      } 
      function rssUpdate() 
      { 
       if(!rssRequest)rssRequest = getRequestObject(); 
       for (var i=0; i<6; ++i) 
       { 
        if (document.forms[0].deals[i].checked == true) 
        { 
         var rss = document.forms[0].deals[i].value; 
         break; 
        } 
       } 
       rssRequest.abort(); 
       rssRequest.open("get","TravelDeals.php?" + "rss" + rss, true); 
       rssRequest.send(null) 
       rssRequest.onreadystatechange=getRssDeals; 
       //clearTimeout(recentDeals); 
       //var recentDeals = setTimeout('rssUpdate()', 5000); 
      } 
      function getRssDeals() 
      { 
       if(rssRequest.readyState==4 && rssRequest.status == 200) 
       { 
        var deals = parseXML(rssRequest.responseXML); document.getElementById("rssFeed").innerHTML = "" 
        var dealsInfo=deals.getElementsByTagName("item"); 
        if (dealsInfo.length > 0) 
        { 
         for (var i=0; i<dealsInfo.length; ++i) 
         { 
          var curHeadline = dealsInfo[i].getElementsByTagName("title")[0].childNodes[0].nodeValue; 
          var curlink = dealsInfo[i].getElementsByTagName("link")[0].childNodes[0].nodeValue; 
          var curPubDate = dealsInfo[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue; 
          var curDesc = dealsInfo[i].getElementsByTagName("description")[0].childNodes[0].nodeValue; 
          var curDeal = "<a href='" + curLink + "'>" + curHeadline + "</a><br/>"; 
          curDeal += curDesc + "<br/>";curDeal += curDesc + "<br/>"; 
          document.getElementById("rssFeed").innerHTML += curDeal; 
         } 
        } 
        else 
         document.getElementById("rssFeed").innerHTML = "The RSS feed appears to be broken or empty."; 
       } 
      } 
     </script> 
    </head> 
    <body onload="rssUpdate()"> 
     <h1>Travel Deals RSS</h1> 
     <form method="get" action=""> 
      <table border="1"> 
       <colgroup span="1" width="125"/> 
       <colgroup span="1" width="350"/> 
       <tr> 
        <td align="top"> 
         <input type="radio" name="deals" value="http://www.orbitz.com/rss/topdeals-vacationpackages.rss.xml" onclick="rssUpdate()"/><Orbitz<br/> 
         <input type="radio" name="deals" value="http://www.travelocity.com/dealservice/globaltrips-shopping-svcs/deals-1.0/services/RssDealsServices?ProductType=Air&rdr=GEN&nm=My~Travelocity~Specials&typ=0&orig=ABQ&dest=PHX,LAS,LAX,NYC,LGB&id=9197I1366355523195" checked="checked" onclick="rssUpdate()"/><Travelocity<br/> 
         <input type="radio" name="deals" value="http://press.hotwire.com/index.php?s=43&pagetemplate=rss" onclick="rssUpdate()"/><Hotwire<br/> 
        </td> 
       </tr> 
      </table> 
      <div id="rssFeed"><div> 
     </form> 
    </body> 
</html> 
+0

找到同樣的東西,這個答案應該可以工作。我不知道你是如何設法得到兩套不同的代碼的。 – Zachrip 2013-04-20 02:30:20

+0

@Zachrip現在我無法追查上一個問題 – 2013-04-20 02:32:08

+0

你是什麼意思「跟蹤」上一個問題? – Zachrip 2013-04-20 02:34:26

0

這是因爲你在td元素中缺少結束報價。當你想要的是<td id="rssFeed" valign="top"></td>時,你有<td id="rssFeed" valign="top></td>。解決這個問題應該會導致元素出現。

相關問題