在几个字符后插入元素

可能重复:
Jquery或javascript在

中添加x个字符后添加一个换行符

我想在元素的内容中插入一个带有五个字符的br标签。 例如,从这个:

 

qwertyuiopasdfghjklzxcvbnm

对此:

 

qwert
yuiopasdfghjklzxcvbnm

我怎么能用jQuery做到这一点?

只需用此函数替换innerHTML(Fires on document.ready()):

 $(document).ready(function() { $('p').html($('p').html().substring(0,5)+'
'+$('p').html().substring(5)); } );

你可以在jfiddle上试试: http : //jsfiddle.net/KwpGr/1/

您可以在$.html()上使用隐式回调:

 /* Be sure to specify which paragraph this applies to */ $("p").html(function(i,v){ return v.replace( /^([\w]{5})/, "$1
" ); });

演示: http : //jsbin.com/orovom/2/edit