字符的css选择器 [英] css selector for character

查看:136
本文介绍了字符的css选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本跨度:

<span class="magic">a sample text</span>

可以创建选择器来更改每个符号a / p>

is it possible to create selector that will change the color of every symbol 'a' or ' '

.magic...<something> {
   color: red;
}

注意:我不想围绕每个字符使用额外的跨度 - 我知道这将工作

note: I do not want to surround every character with additional span - I know this will work

推荐答案

没有办法只用CSS,因为它不提供任何选择器。幸运的是你可以使用javascript和regex代替。

There's no way to do this with CSS only, since it doesn't provide any selectors for this. Fortunately you can use javascript and regex instead.

下面是一个jquery代码的例子,它将替换 p 元素与< span class =red> a< / span> ,所以它可以样式不同(我发现这里代码, stackoverflow,并适应你的需要):

Here's a example of jquery code that will replace every "a" character that's inside a p element with <span class="red">a</span> so it can be styled differently (I found this code somewhere here, in stackoverflow, and adapted it to your needs):

$('p').html($('p').html().replace(/a/g, '<span class="red">a</span>'));

更新的演示

这篇关于字符的css选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆