【发布时间】:2020-01-31 16:35:22
【问题描述】:
如何改变包裹在 div 中的字符串的特定部分的颜色:
我们有一个这样的 div:
<div id="text" class="text-class">Stack Overflow is the largest, most trusted online community for developers </div>
我们如何使用下面的代码模板将“受信任的在线社区”部分的颜色更改为红色?
let text = document.getElementById("text");
// We want to only change the color of this portion of the text
changeColor('trusted online community');
function changeColor(portion){
// code to change the text color
...
// function to animate the color from black to red
function changeToRed(el) {
el.classList.add("colorRed");
}
};
.colorRed {
animation: colorRedAnime 0.8s cubic-bezier(0.785, 0.135, 0.15, 0.86);
animation-fill-mode: forwards ;
}
@-webkit-keyframes colorRedAnime {
from { color: #808080 }
to { color: #e80000 }
}
<div class="container">
<div id="text" class="text-class">Stack Overflow is the largest, most trusted online community for developers </div>
</div>
【问题讨论】:
-
您需要在该部分周围加上
<span class="colorRed">。
标签: javascript html css regex