- DOM (Document Object Model)
- It allow us to manipulate HTML elements.
- 製作動態網頁
-
把
<script>
tag 放在<body>
tag 下面<script src="fileName.js"></script>
-
為什麼要為什麼放在最下面?
- It takes long time to render JS code. (We want to show something to user first)
- Browser has to read all HTML and CSS before using DOM.
可以參考 : The Window Object - w3school
Name | Description |
---|---|
alert() |
彈出警告視窗 |
prompt() |
彈出可以輸入的視窗 |
setInterval() |
間隔固定的延遲重複地時間執行一個函式呼叫或一個程式碼片斷 |
clearInteval( ) |
清除 setInterval() |
addEventListener() |
加入事件監聽 |
“OOP Concept : An object can be a property of another object.”
Name |
---|
Console |
Document |
LocalStorage |
SessionStorage |
上面這四個剛好都是 object,先討論前面兩個,後面兩個請看
- is a property of window object.
- methods:
log()
、warn()
、error()
- 進入本章重點
-
Document is an Object. (Also a property of window object.)
-
Document means the HTML document.
-
This model means all HTML elements are objects. (It means that they all have its properties and methods.)
-
Big picture in DOM
不常用到不討論
getElementsById()
、getElementsByClass()
這兩個不好用,現代少用,因此不多討論。- 現在多用
querySelector()
、querySelectorAll()
addEventListener()
到 8. Events 再討論createElement()
裡面放入 tag name (string) 就可以創建 new objectquerySelector()
- 放入要查詢的 css seletor (string)
- 只會 return 第一個滿足 css selector 條件的 html elements
querySelectorAll()
- 放入要查詢的 css seletor (string)
- 只會 return 第一個滿足 css selector 條件的 html elements
- return a NodeList, not an Array
- Js 中, function declaration 不一定要在使用的 code 上面 (hoisting) 但 arrow function expression 有順序之分
// function declaration
sayHi("rjun");
function sayHi(name) {
console.log("Hi, " + name);
}
// arrow function expression
let sayHi = (name) => {
console.log("Hi, " + name);
};
sayHi("rjun");
-
在 arrow function expression 裡面的
this
指的是Window
Object. -
因此通常通常在 Object 內不會使用 arrow function expression
-
Example
// this keyword let person = { name: "rjun", func() { console.log(this); // this is person Object here. }, arrowFunc: () => { console.log(this); // this is Window Object here. }, }; person.func(); person.arrowFunc();
console
Object { name: "rjun", func: func(), arrowFunc: arrowFunc()} Window http://127.0.0.1:5500/index.html
function to execute on each element (array item).
-
forEach function takes one parameter - function
-
Example
let nums = [215, 12, 512, 24, 56, 1, 7, 91]; nums.forEach(function checkedNum(n) { if (n > 20) console.log(n); });
forEach 內的 function 會自動執行,因此我們不需要設定 function 的名稱 (anonymous function) 也因此可以搭配 arrow function expression
// 搭配 arrow function expression let nums = [215, 12, 512, 24, 56, 1, 7, 91]; nums.forEach((n) => { if (n > 20) console.log(n); });
也可以先在外面定義好 function 直接丟到
forEach()
裡// forEach let nums = [215, 12, 512, 24, 56, 1, 7, 91]; nums.forEach(checkedNum); function checkedNum(n) { if (n > 20) console.log(n); }
-
Array, HTMLCollection and NodeList are similar (
index
andlength
properties) -
Array and NodeList can use
forEach
method, but HTMLCollection cannot. 這也是為什麼現在大家多用 NodeList 而非 HTMLCollection. - 例如:取得所有 class 為 second 的 object 使用getElementsByClassName()
(現少用)// HTMLCollection let secondElement = document.getElementsByClassName("second");
使用
querySelectorAll()
已取得 NodeList// NodeList let secondElement = document.querySelectorAll("second");
-
Different HTML element might have its own methods and property.
-
All HTML element must have properties and methods from this list
Properties and Methods Name addEventListener()
appendChild()
children
childNode
classList ( add()
,remove()
,toggle()
,contains()
)getAttribute()
innerHTML
,innerText
parentElement
querySelector()
querySelectorAll()
remove()
style
children
returns HTML elementschildNode
returns NodeList. (document.querySelectorAll()
also returns NodeList)parentElement
-
innerHTML
跟innerText
後面都接 string,基本功能一樣 -
不同的是
innerHTML
可以放入 html tag,而innerText
被當作純文字 -
.js
中的程式碼可以覆蓋.html
中的,因此可以透過此方式使網頁的 tag 或文字為動態的 -
Example
let h1 = document.querySelector("h1.myH1"); h1.innerHTML = "<mark>This is rjun.</mark>";
-
is a property of element object.
-
is an object that is controlling the CSS styling of an element.
-
hyphen (
-
) in JS is not allowed, therefore, CSS properties are changed to "camelCase" -
JS style object 是 inline-styling ( > id > class > ...)
-
Example 兩種寫法
let button = document.querySelector("button"); button.style = "background-color: red; color: white";
let button = document.querySelector("button"); button.style.backgroundColor = "red"; button.style.color = "white";
- All HTML elements inherit properties and methods from “element object.
- But some of them have its own methods.
- JS event are occurred when things happen in browser. (For example, resize the screen, click on a button, press a key on keyboard.)
- 事件發生時,需要偵探
- JS Events 發生時,需要 event listener.
addEventListener(event type, callback)
- JS Events 都是 Object
Propeties and Methods Name | Description |
---|---|
target |
事件的目標,an Object |
preventDefault() |
如果事件可以被取消,就取消事件的預設行為。但不會影響事件的傳遞,事件仍會繼續傳遞 |
stopPropagation() |
停止 bubble |
若內外層都有 add 一樣 的 event listener ,內層啟動 event 會使外層也啟動 event,會像 bubble 一樣擴散到最外層。不過外層不會影響內層
Example - 點擊內層 b 也會觸發 a
let a = document.querySelector("div.a");
let b = document.querySelector("div.b");
a.addEventListener("click", () => {
alert("a's eventlistener's callback is running");
});
b.addEventListener("click", () => {
alert("b's eventlistener's callback is running");
});
如何停止呢? 使用 stopPropagation()
。把 b 改成以下
b.addEventListener("click", (e) => {
e.stopPropagation();
alert("b's eventlistener's callback is running");
});
學會一點 event 後就可以來製作 dynamic header 了。要用到的有
window.pageYOffset
propertyscroll
eventstyle
object
let header = document.querySelector("header");
let headerAnchor = document.querySelectorAll("header nav ul li a");
window.addEventListener("scroll", () => {
if (window.pageYOffset != 0) {
header.style.backgroundColor = "rgba(0, 0, 0, 0.8)";
header.style.color = "white";
headerAnchor.forEach((a) => {
a.style.color = "white";
});
} else {
header.style = "";
headerAnchor.forEach((a) => {
a.style.color = "";
});
}
});
- Storage is a place to store data in browser (This is not database)
key-value
pair (Both have to be string. If the value is not a string, it would be typecast to a string.)
怎麼看到網頁 storage ?
- FireFox: f12 → Storage
- Chrome: f12 → Application
小補充 Cookies :
Briefly,Cookies are smaller and send server information back with every HTTP request, while LocalStorage is larger and can hold information on the client side.
Methods | Description |
---|---|
setItem(key, value) |
新增 key-value pair |
getItem(key) |
透過 key 取得 value |
removeItem(key) |
透過 key 移除 key-value |
clear() |
清空 storage |
生命週期不同, local > session.
Session storage is “destroyed” once the user closes the browser whereas, Local storage stores data with no expiration data.
- JSON means JavaScript Object Notation
JSON.stringify()
: 將 JSON 檔案變成字串JSON.parse()
:將 string 轉型成 JSON 格式
Object, Array, Boolean .... 都可以用
Example: 把 array 存進 local storage 拿出來還是 array
let friends = ["John", "Evelyn", "Mike"];
localStorage.setItem("friends", JSON.stringify(friends));
let getFriends = JSON.parse(localStorage.getItem("friends"));
Steps:
存進:array → JSON.stringfy()
→ string → setItem()
→ Local Storage
取出:Local Storage → getItem()
→ string → JSON.parge()
→ array