var weathers = items[0].getElementsByTagName("yweather:forecast");
alert(weathers.length);
打印出来为0,我是用webkit内核解析的。
这个网页在IE,FireFox上都没有问题。打印出来是正确值。完整代码:<html xmlns="www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr" clsss=""><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- <link type="text/css" rel="stylesheet" media="all" href="./css/weather.css" /> -->
<embed id="embed1" type="application/x-qt-weather" width=600 height=40/>
<title>NOVATEK</title>
<style type="text/css">
body {background-color:rgb(0, 255, 255)}
tr th, tr td{text-align:center}
tr.alt{background-color:BlueViolet}
tr.white{background-color:AliceBlue}
#cloudy{background-image:url(http://l.yimg.com/a/i/us/we/52/28.gif);
background-repeat:no-repeat;
background-position:center
}
</style>
<script type="text/javascript">
//declare:
var text="";function embeded()//1
{
var embed1 = document.getElementById('embed1');
text = embed1.weather("http://feed43.com/changsha.xml","");
}function exmaple()
{
text += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>";
text += " <rss version=\"2.0\" xmlns:yweather=\"http://xml.weather.yahoo.com/ns/rss/1.0\" xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">";
text += " <channel>";

text += "<title>Yahoo! Weather - Beijing, CH</title>";
text += "<link>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://weather.yahoo.com/forecast/CHXX0008_f.html</link>";
text += "<description>Yahoo! Weather for Beijing, CH</description>";
text += "<language>en-us</language>";
text += "<lastBuildDate>Wed, 28 Dec 2011 12:59 pm CST</lastBuildDate>";
text += "<ttl>60</ttl>";
text += "<yweather:location city=\"Beijing\" region=\"\"   country=\"CH\"/>";
text += "<yweather:units temperature=\"F\" distance=\"mi\" pressure=\"in\" speed=\"mph\"/>";
text += "<yweather:wind chill=\"38\"   direction=\"80\"   speed=\"4\" />";
text += "<yweather:atmosphere humidity=\"26\"  visibility=\"6.21\"  pressure=\"30.42\"  rising=\"0\" />";
text += "<yweather:astronomy sunrise=\"7:33 am\"   sunset=\"4:54 pm\"/>";
text += "<image>";
text += "<title>Yahoo! Weather</title>";
text += "<width>142</width>";
text += "<height>18</height>";
text += "<link>http://weather.yahoo.com</link>";
text += "<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>";
text += "</image>";
text += "<item>";
text += "<title>Conditions for Beijing, CH at 12:59 pm CST</title>";
text += "<geo:lat>39.93</geo:lat>";
text += "<geo:long>116.28</geo:long>";
text += "<link>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://weather.yahoo.com/forecast/CHXX0008_f.html</link>";
text += "<pubDate>Wed, 28 Dec 2011 12:59 pm CST</pubDate>";
text += "<yweather:condition  text=\"Fair\"  code=\"34\"  temp=\"41\"  date=\"Wed, 28 Dec 2011 12:59 pm CST\" />";
text += "<description><![CDATA[";
text += "<img src=\"http://l.yimg.com/a/i/us/we/52/34.gif\"/><br />";
text += "<b>Current Conditions:</b><br />";
text += "Fair, 41 F<BR />";
text += "<BR /><b>Forecast:</b><BR />";
text += "Wed - Sunny. High: 40 Low: 16<br />";
text += "Thu - Mostly Sunny. High: 32 Low: 22<br />";
text += "Fri - Partly Cloudy. High: 35 Low: 19<br />";
text += "Sat - Sunny. High: 39 Low: 18<br />";
text += "Sun - Partly Cloudy. High: 37 Low: 18<br />";
text += "<br />";
text += "<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://weather.yahoo.com/forecast/CHXX0008_f.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>";
text += "(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>";
text += "]]></description>";
text += "<yweather:forecast day=\"Wed\" date=\"28 Dec 2011\" low=\"16\" high=\"40\" text=\"Sunny\" code=\"32\" />";
text += "<yweather:forecast day=\"Thu\" date=\"29 Dec 2011\" low=\"22\" high=\"32\" text=\"Mostly Sunny\" code=\"34\" />";
text += "<yweather:forecast day=\"Fri\" date=\"30 Dec 2011\" low=\"19\" high=\"35\" text=\"Partly Cloudy\" code=\"30\" />";
text += "<yweather:forecast day=\"Sat\" date=\"31 Dec 2011\" low=\"18\" high=\"39\" text=\"Sunny\" code=\"32\" />";
text += "<yweather:forecast day=\"Sun\" date=\"1 Jan 2012\" low=\"18\" high=\"37\" text=\"Partly Cloudy\" code=\"30\" />";
text += "<guid isPermaLink=\"false\">CHXX0008_2012_01_01_7_00_CST</guid>";
text += "</item>";
text += "</channel>";
text += "</rss>";
text += "<!-- api2.weather.sg1.yahoo.com uncompressed/chunked Tue Dec 27 21:50:03 PST 2011 -->";}
function parsexml()//2
{
try //Internet Explorer  
{    
   xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(text);
    //document.write("windows ");
}catch(e)
{   
    try //Firefox, Mozilla, Opera, etc.    
    {
       parser=new DOMParser();
    xmlDoc=parser.parseFromString(text,"text/xml");
    //document.write("linux ");    
    }  
    catch(e)
    {
      alert(e.message)
    }  
}
}
//document.write("xmlDoc is loaded, ready for use");
function show()//3
{
var temp = "";
root = xmlDoc.getElementsByTagName("channel")[0];
//alert(root);
title = root.getElementsByTagName("title")[0];
//alert(title);
items = root.getElementsByTagName("item");
//alert(items[0]);
//var map = items[0].getElementsByTagName("description")[0].getElementsByTagName("CDATA")[0];
//alert(map);
//var mod = /http://l.yimg.com/a/i/us/we/52/28.gif/;
//var dxx = / /;

var weathers = items[0].getElementsByTagName("yweather:forecast");
alert(weathers.length);
temp = "<table><tr class=\"alt\"><th>Day</th><th>date</th><th>low</th><th>high</th><th>text</th><th>code</th></tr>";
temp += "<tr><td>123</td></tr>";
for(var i=0; i<weathers.length; ++i)
{
temp += "<tr class=\"white\"><td>" + weathers[i].getAttribute("day") + "</td><td>" + weathers[i].getAttribute("date") + "</td><td>" + weathers[i].getAttribute("low") + "</td><td>" + weathers[i].getAttribute("high") + "</td><td id=\"cloudy\">" + weathers[i].getAttribute("text") + "</td><td>" + weathers[i].getAttribute("code") + "</td></tr>";
}
temp += "</table>"; 
document.write(weathers.length);
document.write(temp);





/*temp += "<table class=\"bigwhite\"><tr><td>" + title.firstChild.nodeValue + "</td></tr>";
for(var i = 0; i <= items.length - 1; i++)
{
   item = items[i];
   //***********get titile and description******************
   title = item.getElementsByTagName("title")[0].firstChild.nodeValue;
   link = item.getElementsByTagName("link")[0].firstChild.nodeValue;
   description = item.getElementsByTagName("description")[0].firstChild.nodeValue;
   var reCat = / /;
   var adddata = description.split(reCat);
   temp += "<tr><td class=\"white\">" + title + description + "</td></tr>";
}
temp += "</table>";*/
//document.write(temp);
document.getElementById("rsscontainer").innerHTML = temp;
}//////////////////////////////////////////////////////////
exmaple();
parsexml();
show();
</script>
</head><body><!-- <script type="text/javascript" src="./js/weather.js"></script> -->
<!-- <script type="text/javascript" src="./test.js"></script> -->
<!-- <div id="rsscontainer"></div> -->
<!-- <div class="citydescription" id="showtable"></div> -->
</body></html>

解决方案 »

  1.   

    如果把<yweather:forecast>替换成<yweatherForecast>就行
    'yweather:forecast'
    是因为这里的:所以出错,webkit把这里的:理解成了别的意思
    我试了用\:转义,但是也不行,所以可以把标签名改了,这个方法最省事,要么你就用
    var a = item[0].children;
    for(var i=0 ; i<a.length ; i++){
      
    }
    来遍历查找yweather:forecast
    又或者你找到更好的方法?
      

  2.   

    csdn现在难用死了,丢个密码功能总没丢吧