将链接复制到另一个元素

时间:2015-07-10 10:53:57

标签: jquery wordpress

Wordpress在' .blog-info'中创建了一个链接。我想在页面下方复制到空白链接(.new-link):

git remote add origin ssh://git@bitbucket.org/urRepository/urproject.git #you would get your repo link from bitbucket.
git push -u origin master # to push changes for the first time

有多个共享相同类的多个div,我希望每个.new-link从最近的.blog-info a复制。

在想:

<div class="blog-wrapper">
    <div class="blog-info"><a href="http://foo.com/deeplink/" title="link title"> Title to story</a><br>
    <div class="blog-date">Leeds, UK, 29th June 2015</div>
    <a class="new-link" href="/">more</a>
    </div>
</div>

然而,这仅适用于第一个链接。我需要它从最近的&#39; .blog-info a&#39;进行上下文复制。见JSFiddle:

https://jsfiddle.net/63zhvj1o/

3 个答案:

答案 0 :(得分:3)

无需使用each进行迭代。您可以使用attrhref属性设置为相对anchor

$('.new-link').attr('href', function() {
    return $(this).closest('.blog-info').children('a').attr('href');
});

Demo

答案 1 :(得分:2)

您需要迭代每个锚元素,然后从兄弟锚元素设置相关的href:

 $('a.new-link').each(function(){
        $(this).attr('href',$(this).siblings('a').attr('href'))          
 });

<强> Working Demo

答案 2 :(得分:0)

$('a.new-link').each(function(){
    debugger;
           $(this).attr('href',$(this).siblings('a').attr('href'))          
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div class="blog-wrapper">
    <div class="blog-info">
        <a href="http://foo.com/deeplink1/" title="link title"> Title to story</a><br>
        <div class="blog-date">Leeds, UK, 29th June 2015</div>
        <a class="new-link" href="/">more</a>
    </div>
</div>
<br>
<div class="blog-wrapper">
    <div class="blog-info">
        <a href="http://foo.com/deeplink2/" title="link title"> Title to story</a><br>
        <div class="blog-date">Leeds, UK, 29th June 2015</div>
        <a class="new-link" href="/">more</a>
    </div>
</div>
<br>   
 <div class="blog-wrapper">
    <div class="blog-info">
        <a href="http://foo.com/deeplink3/" title="link title"> Title to story</a><br>
        <div class="blog-date">Leeds, UK, 29th June 2015</div>
        <a class="new-link" href="/">more</a>
    </div>
</div>