1
//獲取偽元素
// CSS代碼 2 #myId:before { 3 content: "hello world!"; 4 display: block; 5 width: 100px; 6 height: 100px; 7 background: red; 8 } 9 // HTML代碼 10 <div id="myId"></div> 11 // JS代碼 12 var myIdElement = document.getElementById("myId"); 13 var beforeStyle = window.getComputedStyle(myIdElement, ":before"); 14 console.log(beforeStyle); // [CSSStyleDeclaration Object] 15 console.log(beforeStyle.width); // 100px 16 console.log(beforeStyle.getPropertyValue("width")); // 100px 17 console.log(beforeStyle.content); // "hello world!
//更改樣式:
1 // CSS代碼 2 .red::before { 3 content: "red"; 4 color: red; 5 } 6 .green::before { 7 content: "green"; 8 color: green; 9 } 10 // HTML代碼 11 <div class="red">內容內容內容內容</div> 12 // jQuery代碼 13 $(".red").removeClass('red').addClass('green');