2010-02-07 27 views
4

我不能在這個例子中遍歷一個自定義HTML字符串使用jQuery,如:遍歷自定義HTML字符串使用jQuery

html = '<a href="http://www.site.com"><img width="800" src="http://www.site.com/pic.jpg" alt="" /></a><br /><br />Description<br />'; 
found = $(html).find("a").length; 

「發現」返回0,而我希望拿到1

我懷疑我在這裏做的事情真的很愚蠢,但幾個小時後我仍然沒有看到有什麼問題。

回答

8

你需要把你的HTML代碼放到一個「根元素」就像一個DIV:

$("<div>"+html+"</div>").find("a").length 
+0

做工精細,非常感謝! – Omiod