小编Joã*_*oão的帖子

从html字符串获取img元素

我有以下html字符串:

<html>
<head>
</head>
<body>
    <table>
        <tbody>
            <th>
              <tr>
                <img src=""/>
              </tr>
            </th>
        </tbody>
    </table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

有没有办法只获得img元素?

所以在这种情况下我想要一个像这样的字符串<img src=""/>.

更具体的例子:

$wikiDOM = $("<document>"+ wikiHTML+ "</document>");
var infobox = $wikiDOM.find('.infobox').html();
if(infobox != 'undefined' && infobox !== ""){
    var img = infobox.filter( 'img' );
    console.log( img[0].outerHTML );
}
Run Code Online (Sandbox Code Playgroud)

其中wikiHTML是我对json格式的wikipedia api的请求,用于几个searchterms.

编辑:

从img和infobox返回:

image:[object Object] 
infobox[object Object] 
image:[object Object] 
infobox[object Object]
image:[object Object] 
infobox[object Object]
image:[object Object] 
infobox[object Object] 
image:[object Object] 
infobox[object Object]
image:[object Object] 
infobox[object Object]
image:[object …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery html-parsing

0
推荐指数
1
解决办法
2372
查看次数

标签 统计

html ×1

html-parsing ×1

javascript ×1

jquery ×1