hello world
Thank you代碼: <p id="f-1" style="margin: 0"> hello world </p> <b id="f-2"> Thank you </b> <script> function show_a() { var f_a = document.getElementById('f-1'); f_a.style.color = 'red'; } function show_b() { document.getElementById('f-2').style.background ='#bbb'; } function show_c() { var f_c = document.getElementById('f-2'); f_c.style.cssText = 'font-size: 20px; color: purple;'; } function show_a2() { var f_a = document.getElementById('f-1'); f_a.innerHTML = "Welcome ! 映美集"; } </script>
例如: <p id="foo"> hello world </p> <script> var foo = document.getElementById('foo'); // 取得目前頁面上的 foo元素 foo.style.color = 'green'; // 將 <p> 的文字字體顏色改成綠色 foo.style.background = 'gray'; // 將 <p> 的背景顏色改成灰色 </script> 如果 CSS 屬性名稱中有 - (例如 background-color),你可以用 [] 的語法, 或將屬性名稱改用駝峰式 (camel case) 的寫法: foo.style['background-color'] = '#f00'; // 將背景顏色改為紅色 foo.style.marginTop = '100px'; // 將 margin-top 設為 100px
用法: <p id="foo"> hello world </p> <script> var foo = document.getElementById('foo'); alert(foo.style.cssText); // 輸出 "" 空字串,因為 foo 上沒有設定 style 屬性 foo.style.cssText = 'font-size: 20px; color: purple;'; // 將 foo 的字體大小設為 20px、字體顏色設為紫色 alert(foo.style.cssText); // 輸出 font-size: 20px; color: purple; </script>
語法: var style = window.getComputedStyle(element, pseudoElement); 參數 element表示一個HTML DOM元素;pseudoElement是一個選擇性參數,用來指定 pseudo-element。 window.getComputedStyle === document.defaultView.getComputedStyle 兩個都指向同一個方法。 範例: <style> #elem { position: absolute; left: 100px; top: 200px; height: 100px; } </style> <div id="elem" style="top:50px;"> hello world </div> <script> var elemA = document.getElementById('elem'); alert(window.getComputedStyle(elemA).getPropertyValue('height')); //顯示 100px alert(window.getComputedStyle(elemA).getPropertyValue('top')); //顯示 50px pseudo-element 的例子: <style> h3::after { content: ' rocks!'; } </style> <h3> hello world </h3> <script> var h3B = document.querySelector('h3'); alert(getComputedStyle(h3B, ':after').content); //顯示 " rocks!" </script>
語法: Element.currentStyle.cssProperty 其中 cssProperty 是 HTML 元素的 CSS 屬性名稱,採駝峰式 (camel case) 的寫法。 用法: <style> #elem { position: absolute; left: 100px; top: 200px; height: 100px; } </style> <div id="elem" style="top:50px;"> hello world </div> <script> var elemC = document.getElementById('elem'); alert(elemC.currentStyle.height); //顯示 100px alert(elemC.currentStyle.top); //顯示 50px </script>
事件名稱 | 觸發條件 |
---|---|
blur | blur 物件失去焦點時 |
change | 物件內容改變時 |
click | 滑鼠點擊物件時 |
dblclick | 滑鼠連點二下物件時 |
error | 當圖片或文件下載產生錯誤時 |
focus | 當物件被點擊或取得焦點時 |
keydown | 按下鍵盤按鍵時 |
keypress | 按下並放開鍵盤按鍵後 |
keyup | 按下並放開鍵盤按鍵時 |
load | 網頁或圖片完成下載時 |
mousedown | 按下滑鼠按鍵時 |
mousemove | 介於over跟out間的滑鼠移動行為 |
mouseout | 滑鼠離開某物件四周時 |
mouseover | 滑鼠進入一個元素 (包含進入該元素中的子元素) 四周時 |
mouseup | 放開滑鼠按鍵時 |
resize | 當視窗或框架大小被改變時 |
scroll | 當捲軸被拉動時 |
select | 當文字被選取時 |
submit | 當按下送出按紐時 |
beforeunload | 當使用者關閉 (或離開) 網頁之前 |
unload | 當使用者關閉 (或離開) 網頁之後 |
<html> <head> <title>inline event handling example</title> </head> <body> <button onclick="triggerAlert();"> click me </button> // 按下button按鈕,會引發 triggerAlert()事件。 <script> function triggerAlert() { alert('Hey'); // 當使用者點下button按鈕會跳出 "Hey"。 } </script> </body> </html>
<html> <head> <title>inline event handling example</title> </head> <body> <button onclick="triggerAlert(this);" data-name="Mike"> click me </button> // 單點button按鈕,會引發 triggerAlert()事件。 // 傳入 this(代表此元素物件本身) 當參數 <script> function triggerAlert(ele) { alert('Hey ' + ele.getAttribute('data-name')); // 當使用者點下button按鈕會跳出 "Hey Mike"。 } </script> </body> </html>
<html> <head> <title>traditional event handling example</title> </head> <body> <button id="foo"> click me </button> // 按下button按鈕,會引發 triggerAlert()事件。 <script> function triggerAlert() { alert('Hey'); // 當使用者點下button按鈕會跳出 "Hey"。 } var ele = document.getElementById('foo'); ele.onclick = triggerAlert; // 當使用者按下 (id="foo")按鈕時,執行 triggerAlert 函數 </script> </body> </html> 如果要取消綁定事件處理函數,將事件屬性值設為 null 就可以了: ele.onclick = null;
<html> <head> <title> addEventListener example </title> </head> <body> <script> function myAlert() { alert('Hey!'); } document.addEventListener('click', myAlert); // 當使用者用滑鼠點擊頁面時,執行 myAlert 函數 window.addEventListener('load', function() { // 當網頁載入時,執行這個 callback 函數 alert('頁面已載入!'); }); </script> </body> </html> addEventListener 相較於 DOM Level 0 的方法是它可以對一個元素同時指定多個事件處理函數: <html> <head> <title> addEventListener example </title> </head> <body> <script> function myAlert1() { alert('Hey!'); } function myAlert2() { alert('Hello!'); } document.addEventListener('click', myAlert1); // 當使用者用滑鼠點擊頁面時,執行 myAlert1 函數 document.addEventListener('click', myAlert2); // 當使用者用滑鼠點擊頁面時,執行 myAlert2 函數 </script> </body> </html> 在上面的例子中點擊頁面 依序 會跳出 "Hey!" "Hello!"。 但要特別注意的是依不同瀏覽器的實作,執行順序可能不會等於事件處理函數指定的順序。
<html> <head> <title> removeEventListener example </title> </head> <body> <button id="foo"> click me </button> <script> function myAlert() { alert('Hey!'); } var ele = document.getElementById('foo'); ele.addEventListener('click', myAlert); // 綁定 click 事件處理函數 ele.removeEventListener('click', myAlert); // 移除剛綁定的 click 事件處理函數 </script> </body> </html>
<html> <head> <title> event flow example </title> </head> <body> <div> <ul> <li></li> </ul> </div> </body> </html>
element.onclick = function(event) { // ... }; // 或是 element.addEventListener('click', function(event) { // ... }); 在 IE9 以下的 DOM Level 0 或 attachEvent 方法不會傳入 event object,但有一個 global 的 window.event object 可以用來代替, 所以跨瀏覽器 (cross-browser) 的寫法可以改成: element.onclick = function(event) { event = event || window.event; // ... };
屬性 | 說明 |
---|---|
type | 返回事件類型,例如 "click" |
target | 指向觸發事件的 DOM element |
currentTarget | 在 event bubbling 階段中,指向目前執行的事件處理函數是綁定在哪個 DOM element 上 |
timeStamp | 事件發生時的時間 timestamp (單位是 milliseconds 毫秒) |
eventPhase | 返回為一個數字,表示事件處於目前所處的傳播狀態 (event flow),有這些值: 0: None 1: capturing phase 2: target phase 3: bubbling phase |
屬性 | 說明 |
---|---|
which | 當按下滑鼠按鍵,取得是哪個按鍵,可能的值有: 0: 非按鍵 1: 左鍵 2: 中鍵或滾輪 3: 右鍵 |
relatedTarget | 指向參與事件的相關 DOM element。用在 mouseover 事件,表示剛離開的那個 DOM element; 用在 mouseout 事件,表示剛進入的那個 DOM element |
pageX | 當按下滑鼠時 (或觸控螢幕時),取得距離頁面 (document) 最左上角的水平距離 (單位 pixel) |
pageY | 當按下滑鼠時 (或觸控螢幕時),取得距離頁面 (document) 最左上角的垂直距離 (單位 pixel) |
屬性 | 說明 |
---|---|
keyCode | 當 keypress 事件時,返回 character code;當 keydown 或 keyup 事件時,返回 key code |
which | 值同 keyCode |
charCode | 當 keypress 事件時,返回 character code |
altKey | 布林值 (boolean),用來判斷使用者是否有按 alt 鍵 |
ctrlKey | 布林值 (boolean),用來判斷使用者是否有按 ctrl 鍵 |
metaKey | 布林值 (boolean),用來判斷使用者是否有按 meta 鍵 |
shiftKey | 布林值 (boolean),用來判斷使用者是否有按 shift 鍵 |
element.addEventListener('click', function(event) { event.stopPropagation(); // ... }); IE 在 IE9 開始才有支援 stopPropagation。 在 IE9 以下你可以使用 IE 專有的 cancelBubble 屬性 event.cancelBubble = true;。
element.addEventListener('click', function(event) { event.preventDefault(); // ... }); IE 在 IE9 開始才有支援 preventDefault。 在 IE9 以下你可以使用 IE 專有的 returnValue 屬性 event.returnValue = false;。
document.body.addEventListener('click', function(event) {
event.target.style.color = 'yellow'; // 把字體改成黃色
});
document.onkeydown = function(event) { if (event.keyCode === 89 && event.ctrlKey) { alert('你同時按下 "control + y"'); } else if (event.which === 90 && event.ctrlKey ){ alert('你同時按下 "control + z"'); } };
window.onbeforeunload = function(event) {
return '你確定要離開本頁面嗎?'; // 要離開或關閉目前頁面時,返回要顯示給使用者看的提醒文字
});