1
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 

并且 entry-content 类就在标题 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>&nbsp;<a href="http://appfessional.com/new/tag/gear-2/" rel="tag">gear</a>&nbsp;<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 &nbsp;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>
4

2 回答 2

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);
});
于 2013-09-06T15:50:29.283 回答
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 中导航即可找到合适的元素。

于 2013-09-06T15:53:21.153 回答