Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/Jquery:如何获取具有相同类的图像的src值_Javascript_Jquery_Html - Fatal编程技术网

Javascript/Jquery:如何获取具有相同类的图像的src值

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"

如何获取具有相同类的图像的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值):


...........................
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>');
});