2016-11-30 94 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 
});