2016-11-30 46 views
0

越來越節點我有一個HTML字符串jQuery的 - 從HTML字符串

$myhtml = '<div class="hello"><img src="/img/my/pic"/><p>this is a description">'; 

// the above line is made up it is actually much more complex and pulled in via ajax but you get the idea. 


$parsedHtml = jQuery.parseHTML($myhtml). 

我如何拔出各個節點和屬性?具體如何獲取圖片標籤的src屬性?

+0

你要買什麼?你可以在解析對象上使用任何jQuery方法,所以我不確定你在這裏期待什麼?! –

+1

專門查找src屬性。 – LeBlaireau

回答

0

你應該能夠做到這一點下面這個模式:

var html = $.parseHTML(str), 
var nodeNames = []; 

// Gather the parsed HTML's node names 
$.each(html, function(i, el) { 
    nodeNames[ i ] = "<li>" + el.nodeName + "</li>"; 
    //You do your own magic here when src is found 
});