var src = jQuery('.ovh header h2 a').attr("href");
jQuery('.entry-content').after('<a href="' + src_should_be_here + '">Continue reading >></a>');
我正在使用此代码添加所有帖子的链接但我无法从标题中的链接获取href,如下所示:
header > h2 > a
,入口内容类就在标题div之后。 我试过这个:
var src = jQuery('.ovh header h2 a').attr("href");
但它只选择第一篇文章的链接而非每篇文章
HTML:
<header>
<div class="entry-meta post-info">
<!-- <span class="byline author vcard">By <a href="http://appfessional.com/new/author/admin1/" rel="author" class="fn">Kitejock Team</a>,</span>-->
<span class="post-tags"><a href="http://appfessional.com/new/tag/canada/" rel="tag">canada</a> <a href="http://appfessional.com/new/tag/gear-2/" rel="tag">gear</a> <a href="http://appfessional.com/new/tag/np/" rel="tag">np</a></span>
<a href="http://appfessional.com/new/satisfy-your-thirst-with-np-2014-collection/#respond" title="Comment on Satisfy Your Thirst with NP 2014 Collection">Leave a comment</a>
</div>
<h2><a href="http://appfessional.com/new/satisfy-your-thirst-with-np-2014-collection/">Satisfy Your Thirst with NP 2014 Collection</a></h2>
</header>
<div class="entry-content">
<p>NP brings the 2014 collection of waterwear and accessories, all designed to enhance the riding experience and to provide comfort, protection and style to every rider. Share their 40 year obsession for water and decide for yourself. www.npsurf.com</p>
</div>
答案 0 :(得分:1)
迭代.entry-content
元素,然后找到相关的href,并在当前.entry-content
jQuery('.entry-content').each(function() {
var href = $(this).prev('header').find('h2 > a').attr('href'),
anchor = $('<a />', {href: href, text: 'Continue reading >>'});
$(this).after(anchor);
});
答案 1 :(得分:0)
如果我理解正确,你的html看起来像这样:
<div class="ovh">
<div class="header">
<h2>
<a href="some-link">Lorem Ipsum</a>
</h2>
</div>
<div class="entry-content"></div>
</div>
并且您希望找到每个条目内容,之后您想要添加从每个.ovh header h2 a
获得的链接
在这种情况下,你可以这样做:
$('.entry-content').each(function(){
var href = $(this).prev().find('h2 a').attr("href");
$(this).after('<a href="' + href + '">Continue reading >></a>');
});
通过这种方式,您只需浏览DOM即可找到合适的元素。