ocument 對象
每個載入瀏覽器的 HTML 文檔都會成為 Document 對象。
Document 對象使我們可以從腳本中對 HTML 頁面中的所有元素進行訪問。
Document 對象集合
all[] 提供對文檔中所有 HTML 元素的訪問。
anchors[] 返回對文檔中所有 Anchor 對象的引用。 針對<a name=""></a>
applets 返回對文檔中所有 Applet 對象的引用。采用java編程語言編寫的程序
forms[] 返回對文檔中所有 Form 對象引用。
images[] 返回對文檔中所有 Image 對象引用。
links[] 返回對文檔中所有 Area 和 Link 對象引用。
all[] 各瀏覽器對此兼容性很差, 建議不要使用
document.all[i]
document.all[name]
document.all.tags[tagname]
anchors 集合可返回對文檔中所有 Anchor 對象的引用。
document.anchors[]
<html>
<body>
<a name="first">First anchor</a><br />
<a name="second">Second anchor</a><br />
<a name="third">Third anchor</a><br />
<br />
Number of anchors in this document:
<script type="text/javascript">
document.write(document.anchors.length)
</script>
</body>
</html>
forms 集合可返回對文檔中所有 Form 對象的引用。
document.forms[]
<html>
<body>
<form name="Form1"></form>
<form name="Form2"></form>
<form name="Form3"></form>
<script type="text/javascript">
document.write("This document contains: ")
document.write(document.forms.length + " forms.")
</script>
</body>
</html>
images 集合可返回對文檔中所有 Image 對象的引用。
document.images[]
<html>
<body>
<img border="0" src="hackanm.gif" width="48" height="48">
<br />
<img border="0" src="compman.gif" width="107" height="98">
<br /><br />
<script type="text/javascript">
document.write("This document contains: ")
document.write(document.images.length + " images.")
</script>
</body>
</html>
links 集合可返回對文檔中所有 Area 和 Link 對象的引用。
document.links[]
<html>
<body>
<img src="planets.gif" width="145" height="126" usemap="#planetmap" />
<map name="planetmap">
<area id="venus" shape="circle" coords="124,58,8" alt="Venus" href="venus.htm" />
</map>
<br />
Number of links in this document:
<script type="text/javascript">
document.write(document.links.length)
</script>
</body>
</html>
Document 對象屬性
body 提供對 <body> 元素的直接訪問。
對于定義了框架集的文檔, 該屬性引用最外層的 <frameset>。
cookie 設置或返回與當前文檔有關的所有 cookie。
domain 返回當前文檔的域名。
lastModified 返回文檔被最后修改的日期和時間。
referrer 返回載入當前文檔的文檔的 URL。如果當前文檔不是通過超級鏈接訪問的, 則為 null。
title 返回當前文檔的標題。
URL 返回當前文檔的 URL (HTML title 元素中的文本)。
Document 對象方法
close() 關閉用 document.open() 方法打開的輸出流,并顯示選定的數據。
getElementById() 返回對擁有指定 id 的第一個對象的引用。
注意:有些瀏覽器直接使用ID就可以找到對象,建議使用getElementById()
getElementsByName() 返回帶有指定名稱的對象集合。
getElementsByTagName() 返回帶有指定標簽名的對象集合。
open() 打開一個流, 以收集來自任何 document.write() 或 document.writeln() 方法的輸出。
document.open(mimetype,replace)
mimetype 可選。規定正在寫的文檔的類型。默認值是 "text/html"。
replace 可選。當此參數設置后, 可引起新文檔從父文檔繼承歷史條目。
注意: 與window.open(URL,name,features,replace)語法格式是不同的;
write() 向文檔寫 HTML 表達式 或 JavaScript 代碼。(如果write()方法放到事件下使用,則會清空頁面代碼,然后在寫)
writeln() 等同于 write() 方法, 不同的是在每個表達式之后寫一個換行符。
<html>
<head>
<script type="text/javascript">
function createNewDoc()
{
var newDoc=document.open("text/html","replace");
var txt="<html><body>Learning about the DOM is FUN!</body></html>";
newDoc.write(txt);
newDoc.close();
}
</script>
</head>
<body>
<input type="button" value="Write to a new document" onclick="createNewDoc()">
</body>
</html>
writeln() 方法與 write() 方法作用相同,外加可在每個表達式后寫一個換行符。
document.writeln(exp1,exp2,exp3,....)
在編寫 <pre> 標記的內容時,該方法很有用。
Keygen 對象
Keygen 對象代表著HTML form表單的 keygen 字段。
該對象提供了一個安全的方式來驗證用戶。
當提交表單時,私鑰存儲在本地,公鑰發送到服務器。
在 HTML 文檔中的每個 <keygen> 標簽都能創建一個 Keygen 對象。
你可以通過form 表單的elements[]數組來搜索 keygen 字段,或者使用 document.getElementById()。
Keygen 對象屬性
=HTML5新增屬性。
屬性 | 描述 |
---|---|
autofocus | 設置或者返回頁面加載時是否自動獲得焦點。 |
challenge | 設置或者返回keygen字段的challenge屬性值。 |
disabled | 設置或者返回是否用 keytag 字段。 |
form | 返回包含該 keygen 字段的表單。 |
keytype | 設置或者返回keygen字段的keytype屬性值。 |
name | 設置或者返回keygen字段name屬性的值。 |
type | 返回keygen字段是哪種表單元素類型。 |
標準屬性和事件
keygen 對象同樣支持標準 屬性 和 事件。
如您還有不明白的可以在下面與我留言或是與我探討QQ群308855039,我們一起飛!
Input Text 對象
Input Text 對象代表 HTML 中 type="text" 的 <input> 元素。
訪問 Input Text 對象
你可以通過 getElementById() 訪問 type="text" 的 <input> 元素:
var x=document.getElementById("myText");嘗試一下
提示: 同樣,你可以通過查找表單 elements 集合來訪問 Input Text 對象。
創建 Input Text 對象
你可以通過 document.createElement() 方法來創建 type="text" 的 <input> 元素:
var x=document.createElement("INPUT");
x.setAttribute("type", "text");
Input Text 對象屬性
=HTML5 新增屬性。
屬性 | 描述 |
---|---|
autocomplete | 設置或返回文本域的 autocomplete 屬性值 |
autofocus | 在頁面加載后設置或返回文本域是否自動獲取焦點 |
defaultValue | 設置或返回文本域的默認值 |
disabled | 設置或返回文本域是否禁用 |
form | 返回一個對包含文本域的表單對象的引用 |
list | 返回一個對包含文本域的選項列表對象的引用 |
maxLength | 設置或返回文本域中的最大字符數 |
name | 設置或返回文本域的名稱 |
pattern | 設置或返回文本域的 pattern 屬性值 |
placeholder | 設置或返回文本域的 placeholder 屬性值 |
readOnly | 設置或返回文本域是否應是只讀的 |
required | 設置或返回 whether the text field must be filled out before submitting a form |
size | 設置或返回文本域的 size 屬性值 |
type | 返回文本域的表單元素類型 |
value | 設置或返回文本域的 value 屬性值 |
Input Text 對象方法
方法 | 描述 |
---|---|
blur() | 從文本域中移除焦點 |
focus() | 讓文本域獲取焦點 |
select() | 選取文本域的內容 |
標準屬性和事件
Input Text 對象同樣支持標準 屬性 和 事件。
如您還有不明白的可以在下面與我留言或是與我探討QQ群308855039,我們一起飛!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。