我想动态地将一个类添加到定义跨度的特定句子中。
假设我想focusColorClass
在 span 的这句话中添加 CSS 类:
不告诉我你怎么能这样?!
我们怎样才能做到这一点?
我试过这个没有运气!
const selectSpan = document.getElementById('dialogue-span')
const sentence = "How could you do this without telling me?!";
selectSpan.innerHTML = selectSpan.innerHTML.replace(new RegExp('(' + sentence + ')'), `<span class="focusColorClass">${sentence}</span>`);
.focusColorClass {
color: red;
}
<div id="answerSentence" class="answerSentence-class">
<span id="dialogue-span">You pirced her ears. How could you do this without telling me?! Now if I had told you Then it would'nt have been a surprise! now would it?! I think she looks cute... But I'm wrong!!!</span>
</div>
浮云间
婷婷同学_
冉冉说
相关分类