js获取伪元素选择器规定的内容代码实例
分类:实例代码
关于伪元素选择器可以参阅CSS before/E::before一章节。
下面介绍一下如何利用js获取伪元素选择器定义的内容,这种需求在以前可能比较少。
但是随着浏览器的进步,伪元素选择器的实用会越来越广泛。
代码实例如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.pipipi.net/" /> <title>犀牛前端部落</title> <style> .element:before{ content:'pipipi.net'; color:rgb(255,0,0); } </style> <script> window.onload=function(){ var odiv=document.getElementById("show"); var content = window.getComputedStyle(document.querySelector('.element'),':before') .getPropertyValue('content') odiv.innerHTML=content; } </script> </head> <body> <div class="element"></div> <div id="show"></div> </body> </html>
上面的代码实现了我们的要求,代码非常的简单,更多内容可以参阅相关阅读。
相关阅读:
(1).getComputedStyle()方法可以参阅getComputedStyle()和currentStyle一章节。
(2).querySelector()方法可以参阅querySelector()一章节。
