DIV CSS 佈局教程網

JavaScript 常用功能程式碼速查
編輯:JavaScript基礎知識     

這裡整理了一些 JavaScript 操作 HTML 的常用程式碼,方便隨時查閱。

1. 改變 HTML 輸出流

document.write(Date());

2. 改變 HTML 內容

document.getElementById("p1").innerHTML = "New text!";

3. 改變 HTML 屬性

document.getElementById("image").src = "https://hovertree.com/hvtimg/201601/lbau8hqb.png";

4. 改變 HTML 樣式

document.getElementById("p2").style.color = "blue";

5. HTML 事件屬性

<button onclick="displayDate()">點擊這裡</button>
<script>
function displayDate() {
  document.getElementById("de").innerHTML = Date();
}
</script>
<p id="de"></p>

6. 滑鼠事件

<div onmousedown="mDown(this)" onmouseup="mUp(this)"
  style="background-color:green; width:120px; height:20px; padding:40px; color:#ffffff;">
  把滑鼠移到上面
</div>
<script>
function mDown(obj) {
  obj.innerHTML = "謝謝";
  obj.style.backgroundColor = "#1ec5e5";
}
function mUp(obj) {
  obj.innerHTML = "把滑鼠移到上面";
  obj.style.backgroundColor = "green";
}
</script>

7. 建立新節點

var para = document.createElement("p");
var node = document.createTextNode("這是新段落");
para.appendChild(node);
var element = document.getElementById("div1");
element.appendChild(para);

8. 刪除節點

var child = document.getElementById("p1");
child.parentNode.removeChild(child);
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved