在html元素中选择文本并更改样式 [英] select text inside an html element and change style

查看:90
本文介绍了在html元素中选择文本并更改样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<body>
   text1
 <div> text2</div>

</body>

我只需要选择text1并添加一些样式即可.

I need to select text1 only and put some styling..

我尝试了

body{
color:red;
}

但是text1和text2都变成红色.

but both text1, and text2 become red.

我正在寻找类似的东西.

I'm searching for something like.

[css_selector_im_searching] 
{
color:red;
}

谢谢

推荐答案

仅textNodes没有CSS选择器,但是您可以使用javascript包装该节点

There is no CSS selector for textNodes only, but you can wrap the node with javascript

$('body').contents().filter(function() {
    return this.nodeType === 3;
}).wrap('<span class="text" />');

FIDDLE

这篇关于在html元素中选择文本并更改样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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