我怎样才能从图像的src属性复制一个URL到jQuery中的href属性? [英] How can I copy a URL from an image's src attribute to an href attribute in jQuery?
本文介绍了我怎样才能从图像的src属性复制一个URL到jQuery中的href属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的HTML代码
This is my HTML code
<div class="anythingWindow">
<ul class="anythingSlider">
<li class="panel activePage">
<div class="content clearfix" style="width: 100%; height: 100%;">
<a href="http://www.example.com/a.jpg" class="focusedLink">
<img src= "http://www.example.com/a.jpg"/>
</a>
<div class="slider-text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit
<a title="" href="#">Read More</a>
</div>
</div>
</li>
<li class="panel activePage">
<div class="content clearfix" style="width: 100%; height: 100%;">
<a href="http://www.example.com/a.jpg" class="focusedLink">
<img src= "http://www.example.com/a.jpg"/>
</a>
<div class="slider-text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit
<a title="" href="#">Read More</a>
</div>
</div>
</li>
</ul>
</div>
正如您在 div.content> a> img
存在具有特定位置的图片。如何将此图片中的网址复制到 div.slider-text>的
? href
属性中。一个
As you can see in div.content > a > img
there is image with specific location. How can I copy the URL from this image to the href
attribute of div.slider-text > a
?
我尝试了jQuery的 each()
方法,但无法使其正常工作。
I tried jQuery's each()
method, but couldn't get it to work.
推荐答案
尝试类似这种方式;
Try something like this;
$(document).ready(function() {
$('.anythingSlider .panel').each(function() {
var $this = $(this);
// Get src attribute from img
var url = $this.find('.content a img').first().attr('src');
// Set href attribute on a
$this.find('.content .slider-text a').first().attr('href', url);
});
});
这篇关于我怎样才能从图像的src属性复制一个URL到jQuery中的href属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文