将元素中的部分文本包裹起来 [英] Wrap around part of text inside an element
本文介绍了将元素中的部分文本包裹起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案一个选项是使用
替换
方法。<$ c $($('p')。html(function(index,oldHtml){
return oldHtml.replace(/(amet)/ g,'< span> $ 1< / span>');
});
Let's say I have the sentence "Lorem ipsum dolor sit amet, consectetur adipiscing elit." inside a "‹p›" tag. I would like to somehow use javascript/jQuery to create an element around the word "amet", e.g. "Lorem ipsum dolor sit ‹span›amet‹/span›, consectetur adipiscing elit." Is there some sort of way to do this? In the real-world application for this, I won't know what text I want to have an element around, so I can't just modify the HTML.
解决方案
One option is using replace
method.
$('p').html(function(index, oldHtml){
return oldHtml.replace(/(amet)/g, '<span>$1</span>');
});
这篇关于将元素中的部分文本包裹起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文