gpt4 book ai didi

JavaScript/Jquery : How to get the src value of the images with the same class

转载 作者:行者123 更新时间:2023-12-02 18:14:05 25 4
gpt4 key购买 nike

如何获取同一类图像的src值并将其写入/输出到空div中?

例如:

<body>
<img class="class1" src="http://www.mysite.com/img/image1.jpg" width="168" height="168" alt="">
<img class="class1" src="http://www.mysite.com/img/image2.jpg" width="168" height="168" alt="">
<img class="class1" src="http://www.mysite.com/img/image3.jpg" width="168" height="168" alt="">
<img class="class1" src="http://www.mysite.com/img/image4.jpg" width="168" height="168" alt="">
<img class="class1" src="http://www.mysite.com/img/image5.jpg" width="168" height="168" alt="">
<img class="class2" src="http://www.mysite.com/img/image6.jpg" width="168" height="168" alt="">
<img class="class2" src="http://www.mysite.com/img/image7.jpg" width="168" height="168" alt="">
<img class="class2" src="http://www.mysite.com/img/image8.jpg" width="168" height="168" alt="">
<img class="class2" src="http://www.mysite.com/img/image9.jpg" width="168" height="168" alt="">
<img class="class2" src="http://www.mysite.com/img/image10.jpg" width="168" height="168" alt="">

<div id="print-src">
<!-- Print Result Here -->
</div>
</body>

结果(如果我只想检索类为 class1 的图像的 src 值):

<body>
...........................

<div id="print-src">
http://www.mysite.com/img/image1.jpg
http://www.mysite.com/img/image2.jpg
http://www.mysite.com/img/image3.jpg
http://www.mysite.com/img/image4.jpg
http://www.mysite.com/img/image5.jpg
</div>
</body>

最佳答案

试试这个:

var prn = $('#print-src');
$('img.class2').each(function () {
prn.append('<p>' + this.src + '</p>');
});

演示 here

关于JavaScript/Jquery : How to get the src value of the images with the same class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19459214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com