[英]jQuery or JS to trim characters from inside a span
我正在尝试使用jQuery或JS删除跨度内某些文本的结尾。
<h3 class="ms-standardheader ms-WPTitle">
<a href="" tabindex="0" accesskey="W">
<nobr>
<span>Programs [1]</span>
我在SharePoint中生成了15个这样的标题-5个程序,5个临床服务和5个其他支持。 我想从其中每个中删除[x],但这是SharePoint自动生成的,因为它不喜欢具有相同名称的标题。
我尝试了以下迭代:
$('.ms-WPTitle a nobr span').splice(0, -4);
我似乎无法获得跨度内的文本以进行修剪。 有什么建议么?
$('.ms-WPTitle a nobr span').text(function(el, old){
return old.slice(0, -4);
});
.text()
接受一个函数,您可以使用该函数轻松修剪字符
演示: http : //jsfiddle.net/vhTuA/
由于ahren提供了一种方法,您也可以尝试这种方法:您也可以尝试
演示 http://jsfiddle.net/CCvPQ/2/ 或 http://jsfiddle.net/CCvPQ/4/
使用的API:
希望它适合事业:)
码
jQuery.fn.justtext = function() {
return $(this).children().find('span').text();
};
alert($('.ms-standardheader').justtext());
使用.first api
jQuery.fn.justtext = function() {
return $(this).children().find('span').first().text();
};
alert($('.ms-standardheader').justtext());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.