2011-09-24 59 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+匹配一個或多個空格字符。