【问题标题】:Take href from element and add to the src attribute of an image从元素中获取 href 并添加到图像的 src 属性中
【发布时间】:2014-11-06 19:28:37
【问题描述】:

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

<div class="image1">
    <a href="/images/productA.jpg"> </a>
</div>

并在此处将其添加到图像src...

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

【问题讨论】:

标签: javascript jquery attributes attr


【解决方案1】:

你可以试试这个:

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

【讨论】:

    【解决方案2】:

    使用jQuery的.attr方法。

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

    【讨论】:

    • 这行得通。我正在使用 Adob​​e Business Catalyst,想绕过他们的 Lightbox 画廊并使用我自己的。我成功地从每个灯箱图像中提取 url 并将其添加到我的画廊。
    • 太棒了,很高兴能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    相关资源
    最近更新 更多