JavaScript HTML DOM - 改變 CSS

來源:互聯網
上載者:User

標籤:onclick   asc   type   .property   script   元素   pre   使用   property   

HTML DOM 允許 JavaScript 改變 HTML 元素的樣式。

改變 HTML 樣式

如需改變 HTML 元素的樣式,請使用這個文法:

document.getElementById(id).style.property=new style
例子 1

下面的例子會改變 <p> 元素的樣式:

<p id="p2">Hello World!</p><script>document.getElementById("p2").style.color="blue";</script>
例子 2

本例改變了 id="id1" 的 HTML 元素的樣式,當使用者點擊按鈕時:

<h1 id="id1">My Heading 1</h1><button type="button" onclick="document.getElementById(‘id1‘).style.color=‘red‘"></button>

JavaScript HTML DOM - 改變 CSS

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.