Javascript/Jquery:如何获取具有相同类的图像的src值
如何获取具有相同类的图像的src值并将其写入/输出到空div中 例如:Javascript/Jquery:如何获取具有相同类的图像的src值,javascript,jquery,html,Javascript,Jquery,Html,如何获取具有相同类的图像的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"
<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值):
...........................
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
试试这个:
var prn = $('#print-src');
$('img.class2').each(function () {
prn.append('<p>' + this.src + '</p>');
});
var prn=$('#print src');
$('img.class2')。每个(函数(){
prn.append(''+this.src+'');
});
演示差不多了,但根据示例,它们并不意味着是单独的div。@Andy,您可以使用
prn.append(“”+this.src+”)
或prn.append(this.src+'')代码>
var prn = $('#print-src');
$('img.class2').each(function () {
prn.append('<p>' + this.src + '</p>');
});