可能重复:
Jquery or javascript to add one line break <br /> after x amount of characters in a <div>
我想在元素的内容中插入一个br
标记五个字符。例如,从中:
<p>qwertyuiopasdfghjklzxcvbnm</p>
对此:
<p>qwert<br />yuiopasdfghjklzxcvbnm</p>
如何使用jQuery执行此操作?
答案 0 :(得分:1)
只需将innerHTML替换为此函数(Fires on document.ready()):
$(document).ready(function() {
$('p').html($('p').html().substring(0,5)+'<br/>'+$('p').html().substring(5));
}
);
你可以在jfiddle上试试:http://jsfiddle.net/KwpGr/1/
答案 1 :(得分:0)
您可以在$.html()
上使用隐式回调:
/* Be sure to specify which paragraph this applies to */
$("p").html(function(i,v){
return v.replace( /^([\w]{5})/, "$1<br/>" );
});