删除内容并使用jQuery追加类

时间:2013-02-15 18:10:52

标签: jquery

我必须使用专有的cms博客,​​该博客会将其作为包含

的“摘要标记”
  1. elipsis
  2. 动态更改链接(因此链接始终不同)
  3. 静态文字说:点击此处阅读更多内容。
  4. 这是摘要标记当前在页面上呈现的方式:

    <p>Content goes here....<a href="example.htm">Click here to read more.</a></p>
    <p>More content goes here....<a href="anothertest.htm">Click here to read more.</a></p>
    <p>Content goes here....<a href="helloworld.htm">Click here to read more.</a></p>
    

    与此同时,我想用一些jquery控制摘要标记,所以我不仅限于它的刚性显示:

    1. 删除每个ellipsis
    2. 仅在包含省略号的class后添加href,因此不会更新页面上的其他链接。
    3. 这将是非常理想的预期结果。

      <p>Content goes here.<a href="example.htm" class="readmore">Click here to read more.</a></p>
      <p>More content goes here.<a href="anothertest.htm" class="readmore">Click here to read more.</a>
      <p>Content goes here.<a href="helloworld.htm" class="readmore">Click here to read more.</a>
      

      非常感谢您的帮助!

      这就是我得到的,我知道这很糟糕

      $("p").html("...").remove:after.attr(".readmore").attr("href"));
      

1 个答案:

答案 0 :(得分:1)

$('p').each(function () {
    var textNode = $(this).find('a:last-child').addClass('readmore')[0].previousSibling;

    textNode.textContent = textNode.textContent.replace(/\.{3}\s*$/, '');
});

这是小提琴:http://jsfiddle.net/sH2eA/