开发者

Parsing a HTML String without Element ID's

This may be a very simple question, but I haven't found an answer to this question that works in my situation yet. I am extremely new to jQuery and JavaScript, mind you. Anyway, I'm using a jQuery plug-in called "jGFeed" that returns to me an object containing important information about a feed. The code for such looks like this:

$.jGFeed('http://weather.yahooapis.com/forecastrss?p=USOR0186&u=f', function(feed) {
    if(!feed) {
        return false;
    }
    var html = feed.entries[0].content;
}

By this point, the "html" variable will now contain a string of HTML code resembling something like this:

<img src="http://l.yimg.com/a/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 73 F<BR /><BR />
<b>Forecast:</b><BR />
Tue - Showers Late. High: 74 Low: 50<br />
Wed - Scattered Thunderstorms. High: 70 Low: 42<br /><br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Klamath_Falls__OR/*http://weather.yahoo.com/forecast/USOR0186_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Cha开发者_如何转开发nnel</a>)<br/>

Now I've seen a lot of ways to parse HTML strings to get the content I want, but they all refer to an element's class or ID in order to find the desired information. What I really want is the image url (http://l.yimg.com/a/i/us/we/52/28.gif), the current weather text and temperature (Mostly Cloudy, 73 F), and the two days provided along with their temperatures and conditions. Is there a way to extract just this information with jQuery?


You can use jQuery selectors to search within your variable by doing like this:

$(selector,yourVariableWhichHasThatHTML)

So you want to get the image url?

$('img',yourVariableWhichHasThatHTML).attr('src');

But I would guess that in your jGfeed plugin you could straightly access to these via the feed variable? if you are using firebug or chrome, try writing: console.log(feed); after var html = feed.entries[0].content. Then if you open developer tools and console tab, you should see what the feed variable has eaten.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜