我需要从标题中删除前3个字符。我用过:
$(".layered_subtitle").each(function() {
var text = $(this).text();
text = text.replace('06.', '');
$(this).text(text);
我动态加载了带编号的标题:
01.title
02.title
03.title ..
如何分隔要删除的字符?
答案 0 :(得分:1)
到目前为止的答案还可以,但我认为你应该以不同的方式做以后续使用。也许你的计数会延长,所以只需要搜索“。”的第一次出现。然后在那里剪断弦。
text = text.substring(text.indexOf(".")+1);
答案 1 :(得分:0)
只需使用javascript substring:
text = text.substring(3);
答案 2 :(得分:0)
试试这个
text = text.substr(3);
alert(text);
$(this).text(text);
答案 3 :(得分:0)
你可以使用这个(最短的slice
/ substring
/ substr
相关的JavaScript函数,如果只使用带正整数的第一个参数,它们实际上是相同的): / p>
text = text.slice(3);
答案 4 :(得分:0)
你可以使用这样的子串方法:
$(".layered_subtitle").each(function() {
var text = $(this).text();
if(text && text.length > 3){
text = text.substring(3)
$(this).text(text);
}
});
如果您需要在ajax请求成功时执行此操作:
function removeChars(){
$(".layered_subtitle").each(function() {
var text = $(this).text();
if(text && text.length > 3){
text = text.substring(3)
$(this).text(text);
}
});
}
var serializedData = {};//Fill data
$.ajax({
url: "url",
type: "post",//post or get
data: serializedData,
// callback handler that will be called on success
success: function (){
//Add here logic where you update DOM
//(in your case add divs with layered_subtitle class)
//Now there is new data on page where we need to remove chars
removeChars();
}
});
答案 5 :(得分:0)
试试这个
text = text.substring(3)
而不是
text = text.replace('06.', '');
答案 6 :(得分:0)
如果您不知道分隔符(点)的确切位置,您必须找到它:
var newText = oldText.substr(oldText.indexOf('.') + 1);
或者,如果您可以在分隔符后找到许多数字或空格字符,则需要使用正则表达式:
var newText = oldText.replace(/^\d+\.\s*/, "");
两者都可以。
答案 7 :(得分:0)
如果标题至少包含三个字符,则可以删除前三个字符:
if (text.length >= 3) text = test.substring(3);
如果数字符合“nn.xxxx”格式,您可以使用正则表达式删除数字:
text = text.replace(/^\d\d\./, '');