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

时间:2014-11-06 19:28:37

标签: javascript jquery attributes attr

如何获取div class .image1

中的href链接
<div class="image1">
    <a href="/images/productA.jpg"> </a>
</div>

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

<ul class="example">
    <li class="GalleryImage1"> <img src="" > </li>
</ul>

2 个答案:

答案 0 :(得分:4)

你可以试试这个:

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);

答案 1 :(得分:1)

使用jQuery的.attr方法。

找到href的选择器,执行my_href = $(your_selector).attr("href"),然后使用src更改img元素上的$(img_selector).attr("src", my_href) attr

相关问题