如何在一个范围中包装每个单词并保留文本格式?例如br,em,强
我正在使用以下代码从一个范围内的富文本编辑器中包装每个单词,但它正在删除我的所有格式.
$(window).load(function() {
$(".hero_image p").addClass("hero_image_caption");
$('.hero_image p').each(function(){
var text = $(this).text().split(' ');
for( var i = 0, len = text.length; i < len; i++ ) {
text[i] = '<span>' + text[i] + '</span>';
}
$(this).html(text.join(' '));
});
});
Run Code Online (Sandbox Code Playgroud)
小智 6
$('.hero_image p').each(function(){
var text = $(this).html().split(' '),
len = text.length,
result = [];
for( var i = 0; i < len; i++ ) {
result[i] = '<span>' + text[i] + '</span>';
}
$(this).html(result.join(' '));
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4810 次 |
| 最近记录: |