タイトルから最初の3文字を削除する必要があります。私が使用した:
$(".layered_subtitle").each(function() {
var text = $(this).text();
text = text.replace('06.', '');
$(this).text(text);
番号付きのタイトルを動的にロードしました:
01.title
02.title
03.title..
削除する文字を区切る方法は?
タイトルから最初の3文字を削除する必要があります。私が使用した:
$(".layered_subtitle").each(function() {
var text = $(this).text();
text = text.replace('06.', '');
$(this).text(text);
番号付きのタイトルを動的にロードしました:
01.title
02.title
03.title..
削除する文字を区切る方法は?
これまでの答えは大丈夫ですが、将来の使用のために別の方法で行う必要があると思います。カウントが増える可能性があるので、「。」の最初の出現を検索してください。そこで弦を切ります。
text = text.substring(text.indexOf(".")+1);
これを使用できます (最初の引数に正の整数のみを使用する場合に実質的に同じslice
/ substring
/関連の JavaScript 関数の中で最も短い):substr
text = text.slice(3);
次のような部分文字列メソッドを使用できます。
$(".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();
}
});
これを試して
text = text.substring(3)
それ以外の
text = text.replace('06.', '');
セパレーター(ポイント)の正確な位置がわからない場合は、次の場所を見つける必要があります。
var newText = oldText.substr(oldText.indexOf('.') + 1);
または、区切り文字の後に多くの数字またはスペース文字が見つかる場合は、正規表現を使用する必要があります。
var newText = oldText.replace(/^\d+\.\s*/, "");
どちらも機能します。
JavaScript 部分文字列を使用するだけです:
text = text.substring(3);
このようにしてみてください
text = text.substr(3);
alert(text);
$(this).text(text);
タイトルが 3 文字以上の場合は、最初の 3 文字を削除できます。
if (text.length >= 3) text = test.substring(3);
正規表現を使用して、「nn.xxxx」の形式に一致する場合にのみ番号を削除できます。
text = text.replace(/^\d\d\./, '');