更改最后一个字母颜色 [英] Change last letter color
问题描述
示例代码:
<p class="test">string</p>
我想改变最后一个字母的颜色,在这种情况下为g解决方案与css,我不需要一个javascript解决方案。
I want to change the color on the last letter, in this case "g", but I need solution with css, I don't need a javascript solution.
我按字母显示字符串,我不能使用静态解决方案。
I display the string letter by letter and i cant use static solution.
推荐答案
不使用javascript,您唯一的选择是:
Without using javascript, your only option is:
<p class="test">strin<span class="other-color">g</span></p>
编辑您的小提琴链接:
我不太确定为什么你说你不需要一个javascript解决方案,因为你已经有很多。无论如何,在这个例子中,你只需要做一些小的变化。从
I'm not really sure why you said you didn't need a javascript solution, since you have quite a bit of it already. Regardless, in this example, you need to make only a couple small changes. Change line 10 from
elem.text(elem.text() + contentArray[current++]);
到
if ( current == contentArray.length-1 ) {
elem.html(elem.html() + "<span style='color:red'>"+contentArray[current++]+"</span>");
} else {
elem.html(elem.html() + contentArray[current++]);
}
请注意,使用 .html
Note that it's important to use .html()
instead of .text()
now, since there's actually styling going on.
这篇关于更改最后一个字母颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!