2012-09-15 33 views
0

我需要從標題中刪除前3個字符。我用:從標題中刪除前3個字符

$(".layered_subtitle").each(function() { 
var text = $(this).text(); 
text = text.replace('06.', ''); 
$(this).text(text); 

我已經加載動態地職稱編號:

01.title

02.title

03.title ..

如何要分開要刪除的字符?

+0

http://www.w3schools.com/jsref/jsref_substring.asp – LoganPHP

回答

0

嘗試這樣

text = text.substr(3); 
alert(text); 
$(this).text(text); 
+1

第二個參數是不必要的 – NimChimpsky

+0

Thanx的幫助! – LifeIsShort

+0

yah NimChimpsky.i認爲「刪除前3個字符」爲另一個 – Gautam3164

0

你可以使用這個(最短slice/substring/substr的產權相關的JavaScript函數如果只使用這實際上是相同的第一個參數爲正整數):

text = text.slice(3); 
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(); 
    } 
}); 
+0

另外一個問題,函數在ajax調用後不起作用。起初,它刪除前3個字符,但在我使用過濾器加載內容字符再次出現:) – LifeIsShort

+0

你可以發佈一些代碼示例,這不工作? –

+0

這很難解釋。我有產品過濾器的eshop,在那個過濾器中,我在每個標題附近都有這個數字的標題。代碼正在工作,直到我使用過濾器,因爲過濾器正在使用ajax來加載內容。我在這個函數中嘗試過。但它沒有幫助。 – LifeIsShort

1

答案至今都沒事,但我認爲你應該做的它對於未來的使用是不同的也許你的計數會延長,所以只需搜索「。」的第一次出現。然後在那裏切斷繩子。

text = text.substring(text.indexOf(".")+1); 
0

試試這個

text = text.substring(3) 

代替

text = text.replace('06.', ''); 
0

如果你不知道分離器(點)的確切位置,你必須找到它:

var newText = oldText.substr(oldText.indexOf('.') + 1); 

或者,如果您可以在後面找到許多數字或空格字符分隔符你需要使用正則表達式:

var newText = oldText.replace(/^\d+\.\s*/, ""); 

兩者都會工作。

0

您可以刪除前三個字符,如果標題是至少有三個字符:

您可以使用正則表達式,只有當它的格式相匹配「nn.xxxx」刪除的數字:

text = text.replace(/^\d\d\./, '');