js控制偽元素樣式


 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');

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM