使用JavaScript获取伪元素属性

CSS pseudo-elements are incredibly useful -- they allow us to create CSS triangles for tooltips and perform a number of other simple tasks while preventing the need for additional HTML elements.  To this point, these pseudo-element CSS properties have been unreachable by JavaScript but now there's a method for getting them!

CSS伪元素非常有用-它们使我们能够为工具提示创建CSS三角形 ,并执行许多其他简单任务,同时避免了对其他HTML元素的需求。 至此,JavaScript无法访问这些伪元素CSS属性,但是现在有一种获取它们的方法!

Assume your CSS looks like:

假设您CSS看起来像:


.element:before {
	content: 'NEW';
	color: rgb(255, 0, 0);
}


To retrieve the color property of the .element:before, you could use the following JavaScript:

要检索.element:before的color属性,可以使用以下JavaScript:


var color = window.getComputedStyle(
	document.querySelector('.element'), ':before'
).getPropertyValue('color')


Passing the pseudo-element as the second argument to window.getComputedStyle allows access to said pseudo-element styles!  Keep this snippet in your toolbox for years to come -- pseudo-elements will only grow more useful with broader browser support!

将伪元素作为第二个参数传递给window.getComputedStyle允许访问所述伪元素样式! 将此代码段保存在您的工具箱中数年之久-伪元素只有在更广泛的浏览器支持下才会变得越来越有用!

翻译自: https://davidwalsh.name/pseudo-element

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值