繁体   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