1

我的问题是我需要从 event.content 的结果中创建一个字符串。Event.content 向我返回一个包含 html 标签的条目。我可以像 container.innerHTML = event.content 一样使用它。我需要 event.content 作为字符串。我试图做这样的事情:

var a = '' + event.content;

但它不起作用。这里是 event.content 的结果:

<img src="http://image.weather.com/web/common/wxicons/31/30.gif?12122006" alt="" />Partly Cloudy, and 84 &deg; F. For more details?

我无法将其转换为 javascript 中的字符串。是否可以?我也试过字符串(event.content)。好的,我把我的整个代码。

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>  
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1">
<title>Hava Durumu</title>  

 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

  google.load("feeds", "1");
function havabas(){

   var feed = new google.feeds.Feed("http://rss.weather.com/weather/rss/local/TUXX0014? cm_ven=LWO&cm_cat=rss&par=LWO_rss");
  feed.load(function(result) {
    if (!result.error) {
        var entry = result.feed.entries[0];
        var container = document.getElementById("weather");
        var c = entry.content;

        var regex = /<img.*?src="(.*?)".*?>.*?([0-9]+\s*&deg;)/;
        var results = regex.exec(entry.content);
        var html = '<img src= ' + results[1] + ' alt="" /> ' + results[2];

      container.innerHTML = html;

    }
  });
}

function initialize() {
havabas();
setInterval(function(){havabas()},2000);
}
google.setOnLoadCallback(initialize);

</script>
</head>
<body>
<div id="weather"></div>
</body>
</html>
4

3 回答 3

1

您的正则表达式无效。它与来自 的字符串不匹配entry.content。原因是&deg;。将您的代码更改为:

var regex = /<img.*?src="(.*?)".*?>.*?([0-9]+)/;
var results = regex.exec(entry.content);
var html = '<img src= ' + results[1] + ' alt="" /> ' + results[2] + ' &deg;';

演示

于 2012-07-25T13:30:18.480 回答
0

可能您忘记使用反冲转义单引号或双引号?这是一个有效的字符串 -

var string="<img src=\"http://image.weather.com/web/common/wxicons/31/30.gif?12122006\"     alt=\"\" />Partly Cloudy, and 84 &deg; F. For more details?";
于 2012-07-25T13:12:34.703 回答
-1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script>
function get1()
{

var i='<img src="http://image.weather.com/web/common/wxicons/31/30.gif?12122006" alt="" />Partly Cloudy, and 84 &deg; F. For more details?';
var j='\'' +i+ '\'';
alert(j);
}
</script>
<BODY onload='get1();'>

</BODY>
</HTML>

请试试这个

于 2012-07-25T13:31:09.430 回答