开发者

Add line break after some (ex. 8) words

开发者 https://www.devze.com 2023-04-08 17:01 出处:网络
I was searching for a script to put a line break after a number of words. I开发者_开发问答 found this here in stackoverflow: Applying a style to and inserting a line break after the first word in a li

I was searching for a script to put a line break after a number of words. I开发者_开发问答 found this here in stackoverflow: Applying a style to and inserting a line break after the first word in a link

So, i change it a little:

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 />'));


The following would replace the 8th space in a string with a <br>

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

\S+ matches one or more non-space character. \s+ matches one or more space character.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号