繁体   English   中英

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

[英]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.

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