DIV CSS 佈局教程網

JavaScript BOM 與 DOM 常用操作快速整理
編輯:關於JavaScript     

BOM 與 DOM 是瀏覽器端 JavaScript 的兩大核心。BOM 處理瀏覽器視窗與環境,DOM 處理頁面文件節點。

window 物件

window 是 BOM 的核心,可開啟或關閉視窗、顯示提示訊息、設定計時器與操作歷史紀錄。

window.open("page.html");
window.close();
window.alert("hello world");
window.confirm("Are you sure?");
window.prompt("請輸入名稱", "");

setTimeout(function () {
  alert("hello world");
}, 3000);

clearTimeout(timerId);

此外,setInterval 與 clearInterval 可用來建立與清除重複執行的計時器。

history 與 location

history 可用來返回上一頁或前往下一頁。location 則代表目前載入頁面的 URL,可讀取 href、hostname、pathname、search 等資訊。

window.history.back();
window.history.forward();
console.log(location.href);

document 物件

document 是 DOM 的根節點,可取得表單、圖片、連結與元素。

document.links[0];
document.images[0];
document.forms[0];
document.getElementById("id");
document.getElementsByTagName("p");

DOM 節點操作

常用節點方法包括 createElement、createTextNode、appendChild、removeChild、replaceChild、insertBefore 與 cloneNode。屬性操作則可使用 getAttribute、setAttribute、removeAttribute。

掌握這些 API,就能完成大部分頁面元素查詢、建立、修改與刪除工作。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved