简体   繁体   English

如何在 jQuery 中获取 img src?

[英]How get img src in jQuery?

I am beginner in js / jQuery.我是 js / jQuery 的初学者。

I have this code:我有这个代码:

var imageArray = [];
    $(document).on("click", ".showPrv", function () {
        $("#dropzone").each(function () {
            $(".dz-image-preview").each(function () {
                $(".dz-image").each(function () {
                    console.log($(this));
                });
            });
        });
    });

this return me:这还给我:

[Log] k (1) (1, line 734)
0 
<div class="dz-image">
<img data-dz-thumbnail alt="11" src="http://pscms2.test/upload/DZ_TEXT_PAGE/d3320b13a0f9c35bcdc98534b3aba06f.jpeg">
</div>

Prototyp k

[Log] k (1) (1, line 734)
0 
<div class="dz-image">
<img data-dz-thumbnail alt="12" src="http://pscms2.test/upload/DZ_TEXT_PAGE/3c5ed6a66822be7ea490b9e446de1451.jpeg">
</div>

Prototyp k

[Log] k [<div class="dz-image">] (1) (1, line 734)
[Log] k [<div class="dz-image">] (1) (1, line 734)
[Log] k [<div class="dz-image">] (1) (1, line 734)
[Log] k [<div class="dz-image">] (1) (1, line 734)
[Log] k [<div class="dz-image">] (1) (1, line 734)
[Log] k [<div class="dz-image">] (1) (1, line 734)
[Log] k [<div class="dz-image">] (1) (1, line 734)

I need to add src content to the imageArray array.我需要将 src 内容添加到 imageArray 数组。 The array must have unique src values (no duplicates).该数组必须具有唯一的 src 值(无重复)。 How can I do this?我怎样才能做到这一点?

The effect I want to achieve:我想要达到的效果:

imageArray = ['http: //pscms2.test/upload/DZ_TEXT_PAGE/d3320b13a0f9c35bcdc98534b3aba06f.jpeg', 'http: //pscms2.test/upload/DZ_TEXT_PAGE/3c5ed6a66822be7ea490519.j4']

Please help me请帮我

You need to select the img element from the selected element, you can use .find() , then use .attr('src') to get the src attribute value and push to the array with arr.push()您需要 select 来自所选元素的img元素,您可以使用.find() ,然后使用.attr('src')获取 src 属性值并使用arr.push()推送到数组

Try this尝试这个

 var imageArray = []; $(document).on("click", ".showPrv", function () { $("#dropzone").each(function () { $(".dz-image-preview").each(function () { $(".dz-image").each(function () { const src = $(this).find("img").attr("src") if(imageArray.indexOf(src) < 0) imageArray.push(src) }); }); }); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM