2011-09-24 54 views
0

我正在寻找一个脚本来在一些单词后面换行。我在计算器发现这个位置:Applying a style to and inserting a line break after the first word in a link添加换行符(例如8)字

所以,我改变了一点:

var el = $('.wtt'); //where wtt is the class of the elements that you want to add the <br /> 
var text = obj.html(); 
var parts = text.split(' '); 
//number 7 represents 8 words 
$('.wtt').html($('.wtt').html().replace(parts[7],parts[7]+'<br />')); 
+1

任何反对CSS的反对:'.wtt {width:200px;自动换行:打破字;白色空间:正常;}'? –

回答

0

下面将与<br>

var text = 'hello there purple giraffe, how are you doing today.'; 
alert(text.replace(/^((\S+\s+){7}\S+)\s+/, '$1<br>')); 

\S+替换字符串中的第8个空格可以匹配一个或更多的非空间字符。 \s+匹配一个或多个空格字符。