2013-02-15 89 views
0

我必须使用一个专有的CMS博客呈现它的“总结标记”包含删除内容并追加类与jQuery

  1. 一个省略号
  2. 一个动态变化的链接(这样的链接永远是不同的)
  3. 静态文字说:点击这里阅读更多。

这是汇总标记当前如何呈现在页面上:

<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后更新页面上的其他链接。

这将是期望的预期结果。

<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")); 
+0

那么什么或谁“*不更新页面上的其他链接* “? – Alexander 2013-02-15 18:18:39

回答

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/

+0

我认为这需要一个精细打印的地方描述,它会改变意想不到的元素 – Alexander 2013-02-15 18:22:01

+0

应该只取代最后的'...';不_all_; – mshsayem 2013-02-15 18:22:52

+0

我发现这在IE8中不起作用,返回的错误'textContent'为空或不是对象。这在IE9,Firefox,Chrome中都有效。 – Evan 2013-02-15 18:44:44