Fred Wu (@fredwu)
CTO by day, freelancing software consultant by night. Founder of Persumi.
马上订阅 Fred Wu (@fredwu) RSS 更新: https://persumi.com/u/fredwu/feed/rss
[jQuery Tip] Traverse/Parse HTML String
ifredwu@gmail.com (Fred Wu)
2010年4月28日 09:42
When you are getting an HTML string from an external source (e.g. from an AJAX get result) and you want to rip out a certain part of the HTML source, you need to make sure that the ‘certain part’ is not at the top level of the HTML source.
For example, we have the following HTML string:
Hello
World
If we want to get the first paragraph element by using:
// data is the HTML source
$('span#first', data)
The above code won’t work, because the p tags are at the top level. Instead, we can simply wrap the HTML source with a div tag and that’ll do it. :)