找到HREF并使h3标题点击进入该链接

时间:2014-09-23 13:52:21

标签: jquery

我的HTML

    //box one
<div class="blog-module">
<h3>Title of Blog</h3>
<div class="blog entry">
Each blog post in here.
</div>
<div class="blog entry">
Each blog post in here.
</div>
<div class="more">
<a href="link.com">Read more</a>
</div>
</div>

//box two
<div class="blog-module">
<h3>Title of Blog</h3>
<div class="blog entry">
Each blog post in here.
</div>
<div class="blog entry">
Each blog post in here.
</div>
<div class="more">
<a href="link.com">Read more</a>
</div>
</div>

使用jQuery,我想找到每个 .more a href并在每个 .blog-module div中生成 h3 标记点击它。 感谢

2 个答案:

答案 0 :(得分:0)

$('.more a', '.blog-module').each(function(index, link){
    var $link = $(link);
    var $headingLink = $('<a/>');
    $headingLink.attr('href', $link.attr('href'));
    var $heading = $('h3', $link.parent().parent());
    $headingLink.append($heading.clone());
    $heading.replaceWith($headingLink);
});

答案 1 :(得分:0)

$('.blog-module').each(function(){
var SP_Link = $(this).find(".more a").attr('href');
var SP_Module_Name = $(this).find("h3").text();
$(this).find("h3").replaceWith ('<h3><a href="'+SP_Link+'">'+SP_Module_Name+'</a></h3>');
});

fiddle