用jquer怎么提取这个页面的信息并返回一个json形式

<body>
    <div class="wrap">
        <img src="../img/head1.jpg" alt="">
        <h6>外星人</h6>
        <p>来自外星的外星人</p>
    </div>
    <div class="wrap">
        <img src="../img/head2.jpg" alt="">
        <h6>外星人2</h6>
        <p>来自外星的外星人2</p>
    </div>
    <div class="wrap">
        <img src="../img/head3.jpg" alt="">
        <h6>外星人3</h6>
        <p>来自外星的外星人3</p>
    </div>
    <input type="button" value="获取">
</body>

问下用jQuery怎么获取每个wrap下面img的src值,h6的文本,p的文本?
并返回成这样的结构

[{pos: ../img/head1.jpg, name: 外星人, content:来自外星的外星人},{pos: ../img/head2.jpg, name: 外星人2, content:来自外星的外星人2},{pos: ../img/head3.jpg, name: 外星人3, content:来自外星的外星人3}]
阅读 2.7k
3 个回答
var result=[];
$(".wrap").each(function(i,item){
    result.push(
    {
        pos:$(item).find("img").attr("src"),
        name:$(item).find("h6").html(),
        content:$(item).find("p").html()
    
    });
})

console.log(result);

自己可以参考手册多练练~

$(".wrap").each(function(i,dom){
    var $this = $(dom),
        pos = $this.children("img").attr("src"),
        name = $this.children("h6").text(),
        content = $this.children("p").text();
    str.push({pos:pos,name:name,content:content});
});

clipboard.png

    var res = [];
        $('.wrap').each(function(){
            res.push({
                pos:$('img',this).attr('src'),
                name:$('h6', this).text(),
                content:$('p',this).text()
            })
        })
    console.log(res)

题外话,这样的东西用模板或者Vue之类的框架要简单很多 \(^o^)/~

推荐问题