简体   繁体   中英

How to get the src of img tag using jquery

I have an HTML as follow :

<ul class="thumbnails">
    <li><a class="thumbnail" href="http://localhost/upload/image/cache/catalog/demo/canon_eos_5d_1-500x500.jpg" title="Canon EOS 5D"><img src="http://localhost/upload/image/cache/catalog/demo/canon_eos_5d_1-228x228.jpg" title="Canon EOS 5D" alt="Canon EOS 5D"></a></li>
    <li class="image-additional"><a class="thumbnail" href="http://localhost/upload/image/cache/catalog/demo/canon_eos_5d_2-500x500.jpg" title="Canon EOS 5D"> <img src="http://localhost/upload/image/cache/catalog/demo/canon_eos_5d_2-74x74.jpg" title="Canon EOS 5D" alt="Canon EOS 5D"></a></li>
    <li class="image-additional"><a class="thumbnail" href="http://localhost/upload/image/cache/catalog/demo/canon_eos_5d_3-500x500.jpg" title="Canon EOS 5D"> <img src="http://localhost/upload/image/cache/catalog/demo/canon_eos_5d_3-74x74.jpg" title="Canon EOS 5D" alt="Canon EOS 5D"></a></li>
</ul>

I want to get the src value img. I have used the following code :

var imgTag =  $('.thumbnail').attr("href");
        console.log(imgTag);

It gives the href of the a tag but I want the src of the img tag just after the a tag what modification should I do ?

image is child element of .thumbnail . You need to use:

$('.thumbnail').find('img').attr("src");

Change SRC of image:

 $('.thumbnail').find('img').attr("src", newSRC);

To get the value of the src attribute of your image, you need to request the src attribute, not the href attribute.

To get the child of an element, use the child (direct descendant) selector parent > child .

In your case:

$('.thumbnail > img').attr('src');

您可以使用以下来源

$('.thumbnail img').attr('src');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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