1

我在 Firefox、android 操作系统和其他浏览器中运行时遇到问题。有谁知道为什么?我尝试了不同的解析,并且查看了一堆文档,包括https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest

function callNews()
{
   var url = "someUrlHere";
   var xmlhttp = new XMLHttpRequest();
   var xmlDoc = null;

for (var i = 1; i < 20; i++)
{

    xmlhttp.open("GET", url, false);
    xmlhttp.setRequestHeader('Content-Type', 'text/xml'); 
    xmlDoc = xmlhttp.responseXML;

    var url2 = xmlDoc.getElementsByTagName("link")[i].textContent;
    var newsTitle = xmlDoc.getElementsByTagName("title")[i].textContent;
    var des = xmlDoc.getElementsByTagName("description")[i].textContent;
    //var newsDate = xmlDoc.getElementsByTagName("pubDate")[i].textContent;
    document.getElementById("desc" + i).innerHTML = newsTitle + "<br /><i style=\"color:red;\">" + des + "</i><br /><a href=\"" + url2 + "\">" + url2 + "</a>" + "<br /><br />" + "<iframe src=\"https://www.facebook.com/plugins/like.php?href=http://www.website.com\"scrolling=\"no\" frameborder=\"0\"style=\"border:none; width:450px; height:80px\"></iframe>";
    xmlhttp.send();
}

document.getElementById("desc" + 1).innerHTML = "Testing";
}

更新了代码...仍然无法正常工作

4

1 回答 1

1

这对我来说效果更好

function callNews()
{
var url = "urlGoesHere";

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    alert("Old");
}
xmlhttp.open("GET", url, false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var x = xmlDoc.getElementsByTagName("title");
var d = document.createElement("div");
var news = new Array();
var word = "";
for (var i = 0; i < x.length; i++)
{
    news[i] = "<a href=\"" + xmlDoc.getElementsByTagName("link")[i].childNodes[0].nodeValue + "\" style=\"text-decoration:none;\"  >" + xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue + "</a>";
    news[i] = "<b>" + news[i] + "</b><br /><em style=color:#555;> " + xmlDoc.getElementsByTagName("description")[i].childNodes[0].nodeValue + "</em>";
    word = word + " " + news[i] + "<br /><br />"
}
document.getElementById("newsUpdate0").innerHTML = word;
}
于 2013-03-31T22:38:53.407 回答