獲取僞元素的屬性值spa
獲取僞元素的屬性值能夠使用window.getComputedStyle()方法,獲取僞元素的CSS樣式聲明對象。而後利用getPropertyValue方法或直接使用鍵值訪問均可以獲取對應的屬性值。對象
語法:window.getComputedStyle(element, [pseudoElement])element
js語法實例:rem
var mydiv=document.querySelector('#mydiv');get
var fontSize=window.getComputedStyle(mydiv,'::before').getPropertyValue('font-size');//獲取before僞元素的字號大小io
更改僞元素的樣式class
經過更換class來實現僞元素屬性值的更改:select
舉個栗子:語法
// CSS代碼方法
.red::before {
content: "red";
color: red;
}
.green::before {
content: "green";
color: green;
}
// HTML代碼
<div class="red">內容內容內容內容</div>
// jQuery代碼
$(".red").removeClass('red').addClass('green');