2013-02-15 165 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