![](/img/trans.png)
[英]How can I copy a URL from an image's src attribute to an href attribute in jQuery?
[英]Take href from element and add to the src attribute of an image
如何获取在div类.image1
找到的href链接
<div class="image1">
<a href="/images/productA.jpg"> </a>
</div>
并将其添加到此处的图像src
...
<ul class="example">
<li class="GalleryImage1"> <img src="" > </li>
</ul>
您可以尝试以下方法:
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);
使用jQuery的.attr
方法。
找到href的选择器,执行my_href = $(your_selector).attr("href")
,然后使用$(img_selector).attr("src", my_href)
更改img
元素上的src
attr。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.