舉例如下
function run() {
var alllink = document.links;
var showdemo = document.getElementById("show");
var i, linkHref, lineBreak;
for(i = 0; i < alllink.length; i++) {
linkHref = document.createTextNode(alllink[i].href);
lineBreak = document.createElement("br");
showdemo.appendChild(linkHref);
showdemo.appendChild(lineBreak);
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:document018.js
功能:示範 JavaScript 程式
作者:張凱慶
時間:西元 2011 年 8 月 */此例中的第 2 行
var alllink = document.links;
變數 (variable) alllink 取得所有連結的 HTMLCollection 物件後,利用迴圈 (loop) 取出所有連結的網址,然後在 id 屬性 (attribute) 為 show 的元素 (element) 中顯示
for(i = 0; i < alllink.length; i++) {
linkHref = document.createTextNode(alllink[i].href);
lineBreak = document.createElement("br");
showdemo.appendChild(linkHref);
showdemo.appendChild(lineBreak);
}我們以下面的 HTML 文件載入
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM DEMO</title>
<script src="document018.js" type="text/javascript"></script>
</head>
<body>
<a href="http://www.example1.com">1</a><br / >
<a href="http://www.example2.com">2</a><br / >
<a href="http://www.example3.com">3</a><br / >
<input type="button" value="RUN" onclick="run();">
<div id="show"></div>
</body>
</html>
<!-- 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:document018.html
功能:示範 JavaScript 程式
作者:張凱慶
時間:西元 2011 年 8 月 -->瀏覽器 (broswer) 開啟,按下 RUN 就會逐行顯示連結網址

| 中英文術語對照 | |
|---|---|
| 物件 | object |
| 變數 | variable |
| 迴圈 | loop |
| 屬性 | attribute |
| 元素 | element |
| 瀏覽器 | broswer |
您可以繼續參考
基本概念
文件物件 document
- 屬性
- document.activeElement
- document.alinkColor
- document.anchors
- document.bgColor
- document.cookie
- document.documentElement
- document.documentURI
- document.forms
- document.images
- document.inputEncoding
- document.lastModified
- document.lastStyleSheetSet
- document.linkColor
- document.links
- document.location
- document.plugins
- document.preferredStyleSheetSet
- document.readyState
- document.referrer
- document.selectedStyleSheetSet
- document.styleSheets
- document.styleSheetSets
- document.title
- document.URL
- document.vlinkColor
- 方法
- document.close()
- document.createAttribute(name_str)
- document.createElement(element_str)
- document.createTextNode(text_str)
- document.enableStyleSheetsForSet(ss_str)
- document.getElementById(id_str)
- document.getElementsByClassName(cn_str)
- document.getElementsByName(n_str)
- document.getElementsByTagName(tn_str)
- document.hasFocus()
- document.open()
- document.write(str)
- document.writeln(str)
相關目錄
HTML DOM 快速導覽
JavaScript 教材
首頁
參考資料
https://developer.mozilla.org/en/DOM/document.links
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-7068919
沒有留言:
張貼留言