开发者

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

I'm rendering an RSS feed and there is content at the beginning 开发者_开发知识库of each content section - it's always 6 characters and I want to use jQuery to remove it.

The content of the particular bit of feed is in a list tag, so <li>blahah Hello this is where I want to display from...</li>

Getting rid of the "blahah" is the goal.

An update:

The jQuery supplied works great but I can't work out why it's pulling all the list elements as if they're inline!

Would you be able to explain why the li tags in this example - jsfiddle.net/GhazG/5 run into each other when rendered, instead of appearing like this - jsfiddle.net/GhazG/6?


This will remove the first 6 characters for every <li> element on the page. I imagine you'll want your selector to specifically target the <li> in question.

   // 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) );

Try it out: http://jsfiddle.net/GhazG/

  • http://api.jquery.com/text
  • http://www.w3schools.com/jsref/jsref_substr.asp

Since you have several <li> elements on the page that need updating, you should do so using an each loop:

Try it out: http://jsfiddle.net/GhazG/7/

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

If the items are being appended to the DOM via javascript, you would also have the option of removing the characters before you do the append. It would probably be a better approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜