伪元素的获取

2020-03-11  本文已影响0人  是小张啊啊
<style>
#box::after{
      display:block;
      content:'456';
}
<style>

<div id = "box">123</div>

伪元素是CSS渲染的,不存在于我们的DOM树中,无法获取,querySelector()返回值为null

var oAf =document.querySelector("#box::after");
var oAf =document.querySelector("#box:after");
console.log(oAf);

getComputedStyle(对象 ,伪类) 括号里传入 (1.对象 2.伪类)
获取样式时采用getComputedStyle(1,2)[attr]这样的方式,若没有伪类,则写为null或者不写

var oBox = getElementById('box');
console.log(getComputedStyle(oBox,':after')['content']);//得到456
console.log(oBox.style[':after']);//不是写在行内里的样式,无法获取得到.
上一篇 下一篇

猜你喜欢

热点阅读