本文源于在osc社区中,有人提问如何用jq获取伪元素。我第一想法是强大的css query应该可以获取伪元素吧。
然而事实上,css query并不能。即我们不能通过$(“:before”)、$(dom).find(“:before”)或document.queryselector(“:before”)来获取:before伪元素。
为此,我不得不重新了解伪元素(pseudo-elements)。为什么不能用js直接获取伪元素呢?
譬如::before和::after伪元素,用于在css渲染中向元素的头部或尾部插入内容,它们不受文档约束,也不影响文档本身,只影响最终样式。这些添加的内容不会出现在dom中,仅仅是在css渲染层中加入。
事实上, 伪元素可以被浏览器渲染,但本身并不是dom元素。它不存在于文档中,所以js无法直接操作它。 而jquery的选择器都是基于dom元素的,因此也并不能直接操作伪元素。
那该怎样操作伪元素的样式呢?
为此,我决定好好总结一下js控制伪元素的方法,方便以后查用。
二. 伪元素有哪些:
首先,先简单说一下伪元素都有哪些。伪元素有六个,分别是 ::after、::before、::first-line、::first-letter、::selection、::backdrop 。
在各大网页中最常用的伪元素,是::after和::before。
这几个伪元素的语意可参考我的另外一篇文章《css伪元素选择器 总结》。
在css3中,建议伪元素使用两个冒号(::)语法,而不是一个冒号 (:),目的是为了区分伪类和伪元素。大多数浏览器都支持这两种表示语法。只有 ::selection 永远只能以两个冒号开头(::)。因为ie8只支持单冒号的语法,所以,如果你想兼容ie8,保险的做法是使用单冒号。
三. 获取伪元素的属性值:
获取伪元素的属性值可以使用 window.getcomputedstyle() 方法,获取伪元素的css样式声明对象。然后利用getpropertyvalue方法或直接使用键值访问都可以获取对应的属性值。
语法:window.getcomputedstyle(element[, pseudoelement])
参数如下:
element(object):伪元素的所在的dom元素;
pseudoelement(string):伪元素类型。可选值有:”:after”、”:before”、”:first-line”、”:first-letter”、”:selection”、”:backdrop”;
举个栗子:
// css代码#myid:before {content: hello world!;display: block;width: 100px;height: 100px;background: red;}// html代码
// js代码var myidelement = document.getelementbyid(myid);var beforestyle = window.getcomputedstyle(myidelement, :before);console.log(beforestyle); // [cssstyledeclaration object]console.log(beforestyle.width); // 100pxconsole.log(beforestyle.getpropertyvalue(width)); // 100pxconsole.log(beforestyle.content); // hello world!
备注:
1.getpropertyvalue()和直接使用键值访问,都可以访问cssstyledeclaration object。它们两者的区别有:
对于float属性,如果使用键值访问,则不能直接使用getcomputedstyle(element, null).float,而应该是cssfloat与stylefloat;
直接使用键值访问,则属性的键需要使用驼峰写法,如:style.backgroundcolor;
使用getpropertyvalue()方法不必可以驼峰书写形式(不支持驼峰写法),例如:style.getpropertyvalue(“border-top-color”);
getpropertyvalue()方法在ie9+和其他现代浏览器中都支持;在ie6~8中,可以使用getattribute()方法来代替;
2.伪元素默认是”display: inline”。如果没有定义display属性,即使在css中显式设置了width的属性值为固定的大小如”100px”,但是最后获取的width值仍是”auto”。这是因为行内元素不能自定义设置宽高。解决办法是给伪元素修改display属性为”block”、”inline-block”或其他。
四. 更改伪元素的样式:
方法1. 更换class来实现伪元素属性值的更改:
举个栗子:
// css代码.red::before { content: red; color: red; }.green::before { content: green; color: green;}// html代码内容内容内容内容
// jquery代码$(.red).removeclass('red').addclass('green');
方法2. 使用cssstylesheet的insertrule来为伪元素修改样式:
举个栗子:
document.stylesheets[0].addrule('.red::before','color: green'); // 支持ie document.stylesheets[0].insertrule('.red::before { color: green }', 0); // 支持非ie的现代浏览器
方法3. 在 标签中插入 ').appendto('head');
五. 修改伪元素的content的属性值:
方法1. 使用cssstylesheet的insertrule来为伪元素修改样式:
var latestcontent = 修改过的内容;var formercontent = window.getcomputedstyle($('.red'), '::before').getpropertyvalue('content'); document.stylesheets[0].addrule('.red::before','content: ' + latestcontent + ''); document.stylesheets[0].insertrule('.red::before { content: ' + latestcontent + ' }', 0);
方法2. 使用dom元素的data-*属性来更改content的值:
// css代码.red::before {content: attr(data-attr);color: red;}// html代码内容内容内容内容
// jacascript代码$('.red').attr('data-attr', 'green');
六. :before和:after伪元素的常见用法总结:
1. 利用content属性,为元素添加内容修饰:
1) 添加字符串:
使用引号包括一段字符串,将会向元素内容中添加字符串。栗子:
a:after { content: after content; }
2) 使用attr()方法,调用当前元素的属性的值:
栗子:
a:after { content: attr(href); }a:after { content: attr(data-attr); }
3)使用url()方法,引用多媒体文件:
栗子:
a::before { content: url(logo.png); }
4) 使用counter()方法,调用计时器:
栗子:
h:before { counter-increment: chapter; cotent: chapter counter(chapter) . }
2. 清除浮动:
.clear-fix { *overflow: hidden; *zoom: 1; }.clear-fix:after { display: table; content: ; width: 0; clear: both; }
3. 特效妙用:
// css代码a {position: relative;display: inline-block;text-decoration: none;color: #000;font-size: 32px;padding: 5px 10px;}a::before, a::after { content: ;transition: all 0.2s;}a::before { left: 0;}a::after { right: 0;}a:hover::before, a:hover::after { position: absolute;}a:hover::before { content: \5b; left: -20px; }a:hover::after { content: \5d; right: -20px; }// html代码我是个超链接
4. 特殊形状的实现:
举个栗子:(譬如对话气泡)
// css代码.tooltip {position: relative;display: inline-block;padding: 5px 10px;background: #80d4c8;}.tooltip:before {content: ;display: block;position: absolute;left: 50%;margin-left: -5px;bottom: -5px;width: 0; height: 0; border-left: 5px solid transparent;border-right: 5px solid transparent;border-top: 5px solid #80d4c8;}// html代码i'm a tooltip.
:before 和 :after 伪元素结合更多css3强大的特性,还可完成非常多有趣的特效和 hack ,这里权当抛砖引玉。
六. 一点小小建议:
伪元素的content属性很强大,可以写入各种字符串和部分多媒体文件。但是伪元素的内容只存在于css渲染树中,并不存在于真实的dom中。所以为了seo优化,最好不要在伪元素中包含与文档相关的内容。
修改伪元素的样式,建议使用通过更换class来修改样式的方法。因为其他两种通过插入行内cssstylesheet的方式是在javascript中插入字符代码,不利于样式与控制分离;而且字符串拼接易出错。
修改伪元素的content属性的值,建议使用利用dom的data-*属性来更改。
以上所述是小编给大家介绍的js控制伪元素的方法汇总,希望对大家有所帮助!
