DOM代表文档对象**模型。HTML 页面在浏览器中呈现。浏览器将从网络服务器下载的页面中包含的所有元素组装到其内存中。一旦完成,浏览器就会在浏览器窗口中显示这些对象,一旦完成,浏览器就无法再识别单个 HTML 元素。支持 JavaScript 的浏览器能够在 HTML 页面在浏览器中呈现之后识别该页面中的各个对象,因为支持 JavaScript 的浏览器可以识别并使用 DOM。因此,允许随意控制对象的功能。
document.alinkColor
document.anchors[0],document.anchors[1],...
document.applets[0],document.applets[1],..
document.bgColor
document.cookie
document.domain
document.embeds[0],document.embeds[1],...
document.fgColor
document.forms[0],document.forms[1],...
document.images[0[,document.images[1],...
document.lastModified
document.linkColor
document.links[0],document.links[1],...
document.location
document.plugins[0],document.plugins[1],...
document.referrer
<!DOCTYPE html>
<html>
<head>
<title>Legacy DOM example</title>
<script type="text/javascript">
function func1() {
var a = document.title;
alert("Document title: " + a);
}
function func2() {
var b = document.URL;
var c = document.lastModified;
var d = document.location;
document.write("Document URL: " + b);
document.write("Document last modified: " + c);
document.write("Document location: " + d);
}
function func3() {
var n = document.forms[1];
document.write("Second form elements: " + n);
}
</script>
</head>
<body>
<center>
<h1 style="color: green">GeeksforGeeks</h1>
<b>Document to try Legacy DOM elements.</b>
<form name="form1">
<p>Section 1:</p>
<button name="b1" id="1"
value="title" onclick="func1()">
Title
</button>
<button name="b2" id="2"
value="INFO" onclick="func2()">
URL
</button>
</form>
<br>
<form name="form2">
Section 2:
<br>
<br>
<button name="bt1" id="1"
value="submit" onclick="func3()">
Elements
</button>
</form>
</center>
</body>
</html>
输出:
W3C DOM: 该 DOM 遵循万维网联盟标准,其中规定:
“文档对象模型是一个平台和语言中立的接口,允许程序和脚本动态访问和更新文档的内容、结构和样式。”*
该模型主要关注语言中立性,以便使脚本编写和样式化文档变得容易。
document.body
document.defaultView
document.documentElement
document.implementation
document.createAttribute(name_of_attr)
document.createComment(some_text)
document.createDocumentFragment()
document.createElement(tagname_of_new_ele)
document.createTextNode(text)
document.getElementById(Id)
document.getElementsByName(name)
document.getElementsByTagName(tagname)
document.importNode(importedNode, deep)
示例:
<!DOCTYPE html>
<html>
<head>
<title>W3c example</title>
<script type="text/javascript">
function hello() {
var n = document.getElementById('2').value;
document.write("Hello " + n);
var a = document.body;
document.write("</br>Document body:" + a);
var b = document.defaultView;
document.write("</br>Document default view: " + b);
}
</script>
</head>
<body>
<center>
<h1 style="color: green">
hello demo!
</h1>
<p>
Hello from GeeksforGeeks, this is
an example representation.
</p>
<form name="myform">
<b>Name:</b>
<input type="text" name="name" id="2">
<input type="submit" name="submit"
value="submit" onclick="hello()">
</form>
</center>
</body>
</html>
输出:
3. IE4 DOM:该DOM是在Internet Explorer版本4中引入的。
后续版本进行了扩展并继续包含 W3C DOM 的功能。
IE4 DOM 中的文档属性:
document.activeElement
document.all[]
此函数用于检查网页中是否存在特定元素,但现在已被视为已弃用,
因为它仅适用于 Internet Explorer,并且在其他浏览器中不受支持。
document.charset
document.children[]
document.defaultCharset
document.expando
document.parentWindow
document.readyState
document.uninitialized
document.loading
document.interactive
document.complete
IE4 DOM 中的文档方法:
document.elementFromPoint(x,y)
Example:
<!DOCTYPE html>
<html>
<head>
<title>IE4 DOM example</title>
<script type="text/javascript">
function func1() {
var a = document.all["greetings"];
alert("Hey, " + a.innerHTML);
}
function func2() {
var b = document.activeElement;
document.write("Active element: " + b);
}
</script>
</head>
<body>
<center>
<h1 style="color: green">
demo
</h1>
<h4 id="greetings">
Hello from GeeksforGeeks, this is
an example representation.
</h4>
<form name="myform">
<input type="submit" name="b1"
value="Greet" onclick="func1()">
<input type="text" name="text1">
<input type="submit" name="b2"
onclick="func2()">
</form>
</center>
</body>
</html>
输出:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有