Javascript:add<;br>;如果发现特殊字符
我的javascript代码返回以下字符串:Javascript:add<;br>;如果发现特殊字符,javascript,Javascript,我的javascript代码返回以下字符串:Al-Kawkab-Al-Ain。 我想在破折号之后添加javascript代码,以便文本以新行开头 我希望达到这样的结果: Al-Kawkab- 艾因 $($('.title a').children().prevObject).each(function (index) { console.log(index + ": " + $( this ).text() ); if ($( t
Al-Kawkab-Al-Ain
。
我想在破折号之后添加javascript代码,以便文本以新行开头
我希望达到这样的结果:
Al-Kawkab-
艾因
$($('.title a').children().prevObject).each(function (index) {
console.log(index + ": " + $( this ).text() );
if ($( this ).text().length > 34) {
// add <br> after dash
}
});
$($('.title a').children().prevObject)。每个(函数(索引){
log(index+”:“+$(this.text());
如果($(this).text().length>34){
//在破折号后添加
}
});
试试这个
var stext = $( this ).text();
if (sText.length > 34) {
var indexOf = $( this ).text().indexOf(' - ');
if(indexOf > -1){
var aTextSplit = sText.split(" - ");
var sNewText = aTextSplit[0];
for(var i=1; i<aTextSplit.length; i++){
sNewText = sNewText + " -" + "\n" + aTextSplit[i];
}
$( this ).text(sNewText);
}
}
@B001我在postHint中添加了:使用
.replace(..)
将“破折号”替换为“破折号和换行符”。您的代码正在某些html上执行操作,您也应该发布str.replace(/\s([-\u*])\ s/g,“$1
”)
@Kashan如果可能的话,我只想使用jQuery
var stext = $( this ).text();
if (sText.length > 34) {
var indexOf = $( this ).text().indexOf(' - ');
if(indexOf > -1){
var aTextSplit = sText.split(" - ");
var sNewText = aTextSplit[0] + " -" + "\n" aTextSplit[1];
$( this ).text(sNewText);
}
}