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

标签 javascript jquery attributes attr

如何获取 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);

关于javascript - 从元素中获取 href 并添加到图像的 src 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26787819/

相关文章:

javascript - Strongloop Passport.js 身份验证 - "Failed to obtain access token"

javascript - 向上滚动时如何淡出我的元素

javascript - 在 JavaScript 中同步加载和评估动态生成的脚本

javascript - 函数调用中的 jQuery 动画

javascript - JQuery:找到一种方法来命名克隆的输入字段

linq - 使用 linq 查询对象的属性

javascript - angularjs 从 $scope 之外访问 ng-model

jquery - 为什么从 Jquery 1.5.1 更改为 Jquery 1.8.1 会破坏我在 MVC3 中的所有 CSS?

html - CSS3 数字匹配选择器

attributes - 通过属性版本控制在 Swagger 中利用 MultipleApiVersions