設(shè)置方法:1、使用“對(duì)象.setAttribute('style', '屬性:值')”語(yǔ)句;2、使用“對(duì)象.style.setProperty('屬性', '值')”語(yǔ)句;3、使用“對(duì)象.style.cssText='屬性:值'”語(yǔ)句。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。
方法1:使用setAttribute()
<p id="p">hello world<p> <button onclick="myFunction()">點(diǎn)擊按鈕,設(shè)置css樣式</button> <script> function myFunction() { document.getElementById("p").setAttribute('style', 'color:#FF0000'); } </script>
效果圖:
方法2:使用setProperty()
<p id="p">hello world<p> <button onclick="myFunction()">點(diǎn)擊按鈕,設(shè)置css樣式</button> <script> function myFunction() { document.getElementById("p").style.setProperty('font-size', '50px'); } </script>
效果圖:
方法3:cssText屬性
<p id="p">hello world<p> <button onclick="myFunction()">點(diǎn)擊按鈕,設(shè)置css樣式</button> <script> function myFunction() { document.getElementById("p").style.cssText='font-size:50px;color: red;'; } </script>
效果圖:
【推薦學(xué)習(xí):javascript高級(jí)教程】