簡體   English   中英

如何使用jQuery刪除標簽內容的前6個字符?

[英]How can I remove the first 6 characters of the contents of a tag with jQuery?

我正在渲染一個RSS feed,每個內容部分的開頭都有內容-它始終為6個字符,我想使用jQuery刪除它。

提要的特定內容在列表標記中,所以<li>blahah Hello this is where I want to display from...</li>

目標是擺脫“ 褻瀆 ”。

更新:

所提供的jQuery效果很好,但我不知道為什么它像內聯一樣拉動所有列表元素!

您能否解釋為什么此示例中的li標記-jsfiddle.net/GhazG/5在呈現時會相互碰撞 ,而不是像這樣-jsfiddle.net/GhazG/6出現?

這將刪除頁面上每個 <li>元素的前6個字符。 我想您會希望選擇器專門針對有問題的<li>

   // Select your <li> element
var $li = $('li');

   // Get the text(), and call .substr() passing the number 6 as the argument
   //   indicating that you want to get the part of the string starting on
   //   index number 6 (the seventh character)
$li.text( $li.text().substr(6) );

試試看: http : //jsfiddle.net/GhazG/

由於頁面上有幾個需要更新的<li>元素,因此應該使用each循環:

試試看: http : //jsfiddle.net/GhazG/7/

$('li').each(function() {
    var $th = $(this);
    $th.text( $th.text().substr(6) );
});

如果項目是通過javascript附加到DOM的,那么執行附加操作之前 ,您還可以選擇刪除字符。 這可能是一個更好的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM