iT邦幫忙

2021 iThome 鐵人賽

DAY 28
0
Modern Web

前端幼鳥三十天養成記系列 第 28

WEB API

DOM Document Object Model 文檔物件模型

DOM的簡歷

  • 職業類別:接口
  • 職稱:處理可擴展標記語言(HTML , XML)
  • 具體工作項目:改變網頁元素(element)的內容屬性(attributes related to HTML tags)和樣式屬性(attributes related to Style)
  • 常見代表作:輪播圖、鼠標移動元素顏色變化
  • DOM的腦袋結構:DOM 樹
<html lang="en">
<head>
    <title>hiP</title>
</head>
<body>
	 <p>我是p</P>
	 <img id ="pImg" src="./p.png">    
</body>
</html>

DOM tree
DOM把所有HTML的內容(元素、屬性、文本、註釋)都看做是物件

Object,因為建成樹(tree),所以用節點(node)表示,元素節點、屬性節點、文本節點、註釋節點。


工作時候的DOM - 操作頁面元素

剛剛聊到,DOM的具體工作項目是,改變網頁element的內容屬性和樣式屬性。換句話說,JS讓頁面動起來。 元素們~嗨起來
促使它動起來的過程,就稱為:事件。

事件有三要素

  • 獲取事件源:誰被觸發了?(WHO)
  • 註冊事件類型:怎麼觸發的?(HOW)
    i.e. 鼠標點擊onclick、鼠標經過 onmouseover
  • 添加事件處理程序:觸發後的響應機制?(WHAT)

獲取事件源

  • document.getElementById("id字串 要記得加引號"){
    return 元素物件 或是 null
    }
  • document.getElementsByTagName("tag name"){
    • elements要加s 因為是集合
    • document 可以換成 父元素
    • return 物件集合
    • return 偽數組(HTMLCollecation),不是null ,undefined
      }
  • document.getElementsByClassName("class name"){
    return 物件集合
    }
  • document.querySelector("tag / .class / #id"){
    return 指定目標的第一個元素物件 (所以不是集合喔~)
    }

註冊事件類型 添加事件處理程序

上例子:將密碼切換成明文
passWord

三要素分析:密碼輸入時是點點。把眼睛點開,密碼變明文

  • 「眼睛」被點 → 事件源:眼睛
  • 眼睛「被點」 → 事件類型:onclick
  • 密碼變明文 → 事件程序 寫function
<body>
    <div>
        <input type="password" placeholder="密碼" ></input>
        <img src="./closeEye.png">
    </div>
    <script>
		//獲取事件
        var icon = document.querySelector("img")
        var input =document.querySelector("input")
        var flag =0;
		//註冊事件類型 添加事件程序
        icon.onclick=function(){
            if(flag==0){
                icon.src="./openEye.jfif";
                input.type="text";
                flag =1;
            } else{
                icon.src="./closeEye.png";
                input.type="password";
                flag =0;
            }            
        }
    </script>
</body>

我們只看html和js的部分,css去旁邊自己玩。

註冊事件類型:

事件源.事件類型 i.e. img.onclick

添加事件類型:

function(){

  • 明文:type="text"; 密碼:type="password"
  • 利用變數flag紀錄程式狀態 (只有0,1)
    }

完整版

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>passWord</title>
    <style>
        img,input{
            margin: 0px;
            padding: 0px;
        }
        div{
            position: absolute;
            border: 1px solid #ccc;
            width: 350px;
            left: 50%;
            top: 40%;
            transform: translate(-50%,-50%);
        }
        input{
            width: 300px;
            color: #ccc;
            outline: none;
            border: 0px;
        }
        img{
            position: absolute;
            width: 30px;
            right: 2px;
            bottom: 0.05px;
        }
    </style>
</head>
<body>
    <div>
        <input type="password" placeholder="密碼" ></input>
        <img src="./closeEye.png">
    </div>
    <script>
        var icon = document.querySelector("img")
        var input =document.querySelector("input")
        var flag =0;
        icon.onclick=function(){
            if(flag==0){
                icon.src="./openEye.jfif";
                input.type="text";
                flag =1;
            } else{
                icon.src="./closeEye.png";
                input.type="password";
                flag =0;
            }            
        }
    </script>
</body>
</html>

延伸閱讀

DAY 6 API與Function的比較 - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天


參考資料

前端web进阶JavaScript核心教程DOM BOM操作


上一篇
stack heap內存、預編譯、作用域鏈 - 概念介紹
下一篇
樣式屬性
系列文
前端幼鳥三十天養成記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言