iT邦幫忙

2021 iThome 鐵人賽

DAY 22
0

What is the HTML DOM?

"The HTML DOM is a standard object model and programming interface for HTML. It defines:

  • The HTML elements as objects
  • The properties of all HTML elements
  • The methods to access all HTML elements
  • The events for all HTML elements
    In other words: The HTML DOM is a standard for how to get, change, add, or delete HTML elements." by W3C School

Further intro: click here.


Example

Before
https://ithelp.ithome.com.tw/upload/images/20210921/20130362Kof0xRcbXz.png
After we click the Lucky Draw button
https://ithelp.ithome.com.tw/upload/images/20210921/201303626Ob8UnBU43.png
The way I coded it:

<!DOCTYPE html>
<html>
<head>
<title>JS - HTML DOM</title>
<script type="text/javascript">
function LuckyDraw() {
    var spanObj=window.document.getElementById("reward"); 
    /*
    This syntax changes the content of an HTML element when it’s needed. 
    "window" can be omitted. 
    We name an id, reward, to revise the body easily. 
    spanObj is a tag Object. It is related with <span id="reward">pineapple cheese</span>. 
    */
 
    spanObj.innerHTML="lemon"; //innerHTML is a property for us to easily modify the content of an HTML element.
    spanObj.style.color="green";
    spanObj.style.fontFamily="Arial"; //When CSS transfers to JS, the second word should be written in capital letter. 
    spanObj.style.fontWeight="bold";
    spanObj.style.display=none;
}
 
</script>
</head>
<body style="font-family:Arial">
    <div>
        Congratulations! You won a piece of <span id="reward">pineapple cheese</span> cake!
    </div>
    <button onclick="LuckyDraw();">Lucky Draw</button>
</body>
</html>

Music of Today: Lover Boy by Phum Viphurit

Yes


Like/Share/Follow

Feel free to comment and share your ideas below to learn together!
If you guys find this article helpful, please kindly do the writer a favor — LIKE this article./images/emoticon/emoticon12.gif


上一篇
#21 JS: Object - Constructors
下一篇
#23 JS: HTML DOM Events - Part 1
系列文
Learn & Play JavaScript -- Entry-Level Front-End Web Development30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言