如何将每个单词都包含在一个span中,同时保留文本格式
16lz
2021-01-22
How can I wrap each word in a span and retain text formatting? e.g. br, em, strong
如何将每个单词都包含在span中并保留文本格式?例如br、新兴市场、强大
I'm using the following code to wrap each word from a rich text editor in a span, but it's stripping out all my formatting.
我使用下面的代码将富文本编辑器中的每个单词打包成一个span,但它将剥离我的所有格式。
$(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(' '));
});
});
1 个解决方案
#1
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(' '));
});
更多相关文章
- 【CodeBase】PHP检查未知媒体文件的格式
- 如何将项添加到json文件格式化数组
- 当将带有撇号的单词保存到数据库时,mysql_real_escape_string会添
- 将STR_TO_DATE格式化为日期
- Laravel 5验证日期为php Y格式。g 2015 ?
- 测试一个字符串是否包含PHP中的单词?
- 以完整二叉树,数组格式获取所有节点
- php执行数据库查询返回json格式数据
- 当excel表格单元格的格式是日期格式非文本格式的时候,phpexcel 应