標籤:映像 img mil 表單 onclick href script order get
使用 document.write() 向輸出資料流寫文本
<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
使用 document.write() 向輸出資料流寫 HTML
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>")
</script>
</body>
</html>
返回當前文檔的標題
<html>
<head>
<title>My title</title>
</head>
<body>
該文檔的標題是:
<script type="text/javascript">
document.write(document.title)
</script>
</body>
</html>
返回當前文檔的 URL
<html>
<body>
該文檔的 URL 是:
<script type="text/javascript">
document.write(document.URL)
</script>
</body>
</html>
返回當前文檔的 referrer
<html>
<body>
<p>referrer 屬性返回載入本文檔的文檔的 URL。</p>
本文檔的 referrer 是:
<script type="text/javascript">
document.write(document.referrer)
</script>
</body>
</html>
返回下載當前文檔的伺服器網域名稱
<html>
<body>
本文檔的網域名稱是:
<script type="text/javascript">
document.write(document.domain)
</script>
</body>
</html>
使用 getElementById()
<html>
<head>
<script type="text/javascript">
function getValue()
{
var x=document.getElementById("myHeader")
alert(x.innerHTML)
}
</script>
</head>
<body>
<h1 id="myHeader" onclick="getValue()">這是標題</h1>
<p>點擊標題,會提示出它的值。</p>
</body>
</html>
使用 getElementsByName()
<html>
<head>
<script type="text/javascript">
function getElements()
{
var x=document.getElementsByName("myInput");
alert(x.length);
}
</script>
</head>
<body>
<input name="myInput" type="text" size="20" /><br />
<input name="myInput" type="text" size="20" /><br />
<input name="myInput" type="text" size="20" /><br />
<br />
<input type="button" onclick="getElements()" value="名為 ‘myInput‘ 的元素有多少個?" />
</body>
</html>
開啟一個新的文檔,添加一些文本,然後關閉它。
<html>
<head>
<script type="text/javascript">
function createNewDoc()
{
var newDoc=document.open("text/html","replace");
var txt="<html><body>學習 DOM 非常有趣!</body></html>";
newDoc.write(txt);
newDoc.close();
}
</script>
</head>
<body>
<input type="button" value="開啟並寫入一個新文檔" onclick="createNewDoc()">
</body>
</html>
返迴文檔中錨的數目
<html>
<body>
<a name="first">第一個錨</a><br />
<a name="second">第二個錨</a><br />
<a name="third">第三個錨</a><br />
<br />
文檔中錨的數目:
<script type="text/javascript">
document.write(document.anchors.length)
</script>
</body>
</html>
返迴文檔中第一個錨的 innerHTML
<html>
<body>
<a name="first">第一個錨</a><br />
<a name="second">第二個錨</a><br />
<a name="third">第三個錨</a><br />
<br />
本文檔中第一個錨的 InnerHTML 是:
<script type="text/javascript">
document.write(document.anchors[0].innerHTML)
</script>
</body>
</html>
計算文檔中表單的數目
<html>
<body>
<form name="Form1"></form>
<form name="Form2"></form>
<form name="Form3"></form>
<script type="text/javascript">
document.write("文檔包含: " + document.forms.length + " 個表單。")
</script>
</body>
</html>
訪問集合中的項目
<html>
<body>
<form id="Form1" name="Form1">
您的姓名:<input type="text">
</form>
<form id="Form2" name="Form2">
您的汽車:<input type="text">
</form>
<p>
要訪問集合中的項目,你既可以使用項目編號,也可以使用項目名稱:
</p>
<script type="text/javascript">
document.write("<p>第一個表單名稱是:" + document.forms[0].name + "</p>")
document.write("<p>第一個表單名稱是:" + document.getElementById("Form1").name + "</p>")
</script>
</body>
</html>
計算文檔中的映像數目
<html>
<body>
<img border="0" src="/i/eg_smile.gif" />
<br />
<img border="0" src="/i/eg_mouse.jpg" />
<br /><br />
<script type="text/javascript">
document.write("本文檔包含:" + document.images.length + " 幅映像。")
</script>
</body>
</html>
HTML DOM 執行個體-Document 對象