从元素获取href并添加到图像的src属性

0 javascript jquery attributes attr

如何获取div类中的href链接 .image1

<div class="image1">
    <a href="/images/productA.jpg"> </a>
</div>
Run Code Online (Sandbox Code Playgroud)

并将其添加到src此处的图片中...

<ul class="example">
    <li class="GalleryImage1"> <img src="" > </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以尝试以下方法:

var href = $('.image1 a').attr('href');

//if you want the href to all img in the ul
$('.example li img').attr('src',href);

//if you want the href only to this li
$('.example .GalleryImage1 img').attr('src',href);
Run Code Online (Sandbox Code Playgroud)