Scripting Documents
Document编程
引用
document.write() inserts text into an HTML document at the location of the <script> tag that contains the invocation of the method.
write()方法在代码所在处插入文档内容
注意在完成文档修改后要调用close()方法
Document Properties
Document的常见属性
bgColor
cookie
domain
lastModifed
location
referrer
title
URL
anchors[], created with an <a> tag that has a name attribute instead of an href attribute.)
applets[]
forms[]
images[]
links[], An array of Link objects that represent the hypertext links in the document.
引用
The href property of a Link object corresponds to the href attribute of the <a> tag: it holds the URL of the link. Link objects also make the various components of a URL available through properties such as protocol, hostname, and pathname.
links数组包含的是有href属性的真正的超链接,anchors数组包含的是有name属性的锚点
引用
Accessible by name property
document.forms[0] // Refer to the form by position within the document
document.forms.f1 // Refer to the form by name as a property
document.forms["f1"] // Refer to the form by name as an array index
document.f1
If a <form> and <img> tag both have the name "n", for example, the document.n property becomes an array that holds references to both elements.
如果documeng下面的元素有相同的name属性,那么document.n得到的是一个包含所有name属性等于“n”的元素数组
引用
Document objects accessed through collections such as document.links have properties that correspond to the attributes of the HTML tag.
Remember that HTML is not case-sensitive, and attributes can be written in uppercase, lowercase, or mixed case. In JavaScript, all event handler properties must be written in lowercase.
Document的下面的元素数组有相应的HTML标签下的属性。HTML是大小写不敏感,而JS是大小写敏感的。
Nodes
节点
引用
The childNodes property of a Node object returns a list of children of the node, and the firstChild, lastChild, nextSibling, previousSibling, and parentNode properties provide a way to traverse the tree of nodes.
Methods such as appendChild(), removeChild(), replaceChild(), and insertBefore() enable you to add and remove nodes from the document tree.
Every Node object has a nodeType property that specifies what kind of node it is.
节点的childNodes属性包含了它所有子节点。
节点其他属性,firstChild, lastChild, nextSibling, previousSibling, parentNode等属性都是方便节点便利的。
Attributes
属性
引用
The attributes of an element (such as the src and width attributes of an <img> tag) may be queried, set, and deleted using the getAttribute(), setAttribute(), and removeAttribute() methods of the Element interface.
HTMLDocument is an HTML-specific subinterface of Document and HTMLElement is an HTML-specific subinterface of Element. Furthermore, the DOM defines tag-specific interfaces for many HTML elements.
The HTMLElement interface defines id, style, title, lang, dir, and className properties.
Some HTML tags, listed in Table 15-2, accept no attributes other than these six and so are fully represented by the HTMLElement interface. All other HTML tags have corresponding interfaces defined by the HTML portion of the DOM specification.
When an HTML attribute name conflicts with a JavaScript keyword, it is prefixed with the string "html" to avoid the conflict. Thus, the for attribute of the <label> tag translates to the htmlFor property of the HTMLLabelElement.
Element接口是提供了getAttribute()、setAttribute()、removeAttribute()方法读取、设置、删除元素的属性的。
HTMLDocument、HTMLElement都是对HTML特定的接口。DOM也定义了一些HTML特定元素的接口。
HTMLElement定义id、style、title、lang、dir、className等通用属性。Tabel 15-2中列出来的一些只接受这六个属性的HTML标签。其他的HTML标签所对应的接口都有一些特定属性。
通常,对应接口的名字都会加上HTML,例如label的对性接口就是HTMLLabelElement
DOM Conformance
引用
One source for conformance information is the implementation itself. In a conformant implementation, the implementation property of the Document object refers to a DOMImplementation object that defines a method named hasFeature().
For example, to determine whether the DOM implementation in a web browser supports the basic DOM Level 1 interfaces for working with HTML documents
document的implemention对象有一个hasFeature()方法,可以帮助确定DOM的实现情况,例如检查DOM 1的HTML部分:
if (document.implementation &&
document.implementation.hasFeature &&
document.implementation.hasFeature("html", "1.0")) {
// The browser claims to support Level 1 Core and HTML interfaces
}
引用
In Internet Explorer 6, hasFeature() returns true only for the feature HTML and version 1.0. It does not report compliance to any of the other features listed in Table 15-3 (although, as shown in Chapter 16, it supports the most common uses of the CSS2 module).
IE 5 and later support the Level 1 Core and HTML features , the key Level 2 CSS features
Unfortunately, IE 5, 5.5, and 6 do not support the DOM Level 2 Events module
Although IE 6 claims (through its hasFeature() method) to support the Core and HTML interfaces of the DOM Level 1 standard, this support is actually incomplete.
IE does not support the node-type constants defined by the Node interface.
IE6不支持检查除了HTML部分的其他模块。IE5之后的版本都支持Level 1 Core和HTML特性,Level 2中的关键CSS特性,不支持Level 2的事件模块。IE也不支持Node中的nodeType的常量。你必须自己定义。
if (n.nodeType == 1 /*Node.ELEMENT_NODE*/) // Check if n is an Element
if (!window.Node) {
var Node = { // If there is no Node object, define one
ELEMENT_NODE: 1, // with the following properties and values.
ATTRIBUTE_NODE: 2, // Note that these are HTML node types only.
TEXT_NODE: 3, // For XML-specific nodes, you need to add
COMMENT_NODE: 8, // other constants here.
DOCUMENT_NODE: 9,
DOCUMENT_FRAGMENT_NODE: 11
};
}
Traversing a Document
引用
In addition to the childNodes property, the Node interface defines a few other useful properties. firstChild and lastChild refer to the first and last children of a node, and nextSibling and previousSibling refer to adjacent siblings of a node.
一个遍历节点的小例子:运用了node的一些遍历属性
function countTags(n) { // n is a Node
var numtags = 0; // Initialize the tag counter
if (n.nodeType == 1 /*Node.ELEMENT_NODE*/) // Check if n is an Element
numtags++; // Increment the counter if so
var children = n.childNodes; // Now get all children of n
for(var i=0; i < children.length; i++) { // Loop through the children
numtags += countTags(children[i]); // Recurse on each one
}
return numtags; // Return the total
}
/**
* getText(n): Find all Text nodes at or beneath the node n.
* Concatenate their content and return it as a string.
*/
function getText(n) {
// Repeated string concatenation can be inefficient, so we collect
// the value of all text nodes into an array, and then concatenate
// the elements of that array all at once.
var strings = [];
getStrings(n, strings);
return strings.join("");
// This recursive function finds all text nodes and appends
// their text to an array.
function getStrings(n, strings) {
if (n.nodeType == 3 /* Node.TEXT_NODE */)
strings.push(n.data);
else if (n.nodeType == 1 /* Node.ELEMENT_NODE */) {
// Note iteration with firstChild/nextSibling
for(var m = n.firstChild; m != null; m = m.nextSibling) {
getStrings(m, strings);
}
}
}
}
- 大小: 69.8 KB
分享到:
相关推荐
JavaScript DOM编程艺术(第2版)是一本深受程序员喜爱的JavaScript技术书籍,专注于讲解如何使用JavaScript与Document Object Model(DOM)进行交互。DOM是Web页面的结构化表示,它允许我们通过编程方式操纵HTML和XML...
JavaScript DOM编程艺术(第2版)是一本深受开发者欢迎的书籍,主要涵盖了JavaScript语言在Web前端开发中的应用,特别是关于DOM(Document Object Model)的深入理解和实践。这本书的随书光盘包含了源代码和PDF电子版...
但是,根据标题《JavaScript DOM编程艺术【第2版&高清】.pdf》和描述“JavaScript DOM编程艺术,高清资源,无比经典,值得拥有”,我们可以推断这本书主要讲述了JavaScript中DOM(文档对象模型)的相关编程技术。...
根据提供的文件信息,我们可以推断出这是一本关于JavaScript DOM编程技术的书籍——《JavaScript DOM编程艺术(第2版)》。尽管实际书籍内容并未给出,但从标题、描述及部分链接信息来看,这本书主要涉及JavaScript...
DOM(Document Object Model)是HTML和XML文档的标准表示,它将文档结构转化为一个可编程的树形模型,使得我们可以用JavaScript来动态修改、添加和删除页面内容。 在JavaScript中,DOM操作主要包括以下几个核心知识...
《DOM Scripting: Web Design with JavaScript and the Document Object Model》是由Jeremy Keith撰写的一本关于JavaScript和DOM编程的经典著作。这本书深入浅出地介绍了如何利用JavaScript动态操控网页内容,提升...
《JavaScript.DOM编程艺术(第2版)》是一本深入探讨JavaScript与DOM(Document Object Model)交互的权威书籍。DOM是Web开发中的核心组件,它允许程序员通过JavaScript来操作HTML和XML文档的结构、内容和样式。这本书...
JavaScript DOM编程艺术第二版是一本深入探讨JavaScript与DOM(Document Object Model)交互的权威书籍,主要面向希望提升JavaScript客户端开发技能的程序员。DOM是Web页面的结构化表示,JavaScript通过DOM API可以...
学习JavaScript高级编程技术,首先需要理解脚本语言的概念。脚本语言是相对于编译型语言而言的,它们通常不需要预编译,而是直接由解释器执行。JavaScript就是这样的脚本语言,它在网页加载时被解释执行,可以实时...
《JavaScript DOM编程艺术》是一本深受开发者欢迎的JavaScript学习书籍,专注于DOM(Document Object Model)这一核心概念。DOM是Web页面的结构模型,通过JavaScript与DOM的交互,开发者可以实现对网页内容的动态...
JavaScript DOM编程艺术第二版是一本深入探讨JavaScript与DOM(Document Object Model)交互的权威书籍,主要面向希望提升JavaScript技能并掌握网页动态效果开发的Web开发者。DOM是HTML和XML文档的编程接口,它允许...
JavaScript DOM编程艺术是一本深入探讨如何使用JavaScript操作和操纵网页文档对象模型(Document Object Model)的经典书籍。DOM是HTML和XML文档的一种结构化表示,它允许我们通过编程方式访问和修改网页内容。这...
《JavaScript DOM编程艺术 第2版》是一本深入探讨JavaScript与DOM(Document Object Model)交互的权威书籍。DOM作为Web页面的结构模型,是JavaScript进行页面动态操作的核心工具。这本书全面覆盖了利用JavaScript来...
JavaScript DOM编程艺术是一本深入浅出的编程指南,专注于JavaScript语言与DOM(Document Object Model)的交互。DOM是HTML和XML文档的结构化表示,它允许我们通过编程方式访问和修改网页内容。这本书适合JavaScript...
1. **DOM操作**:Document Object Model(DOM)是HTML和XML文档的结构化表示,JavaScript可以通过DOM API来操作页面元素,如添加、删除或修改元素内容。 2. **事件处理**:JavaScript可以监听并响应用户的各种交互...
《JavaScript DOM编程艺术 第2版》是一本深入探讨JavaScript与DOM(Document Object Model)交互的权威书籍。DOM是Web页面的结构模型,JavaScript通过DOM API能够对网页内容进行动态操作,实现丰富的交互效果。这...
目录: 第一章 javascript语言概述 第二章 JavaScript语言基础 第三章 JavaScript事件处理 第四章 JavaScript基于对象编程 第六章 string,math,array等数据对象 第七章 window及相关顶级对象 第八章 document对象
5. JavaScript的服务器端编程:这是本书的一个特色部分,讲解了如何使用JavaScript进行服务器端编程,这部分内容对于初学者来说难度较大,但为学习者打开了JavaScript应用的新世界。 6. 其他高级主题:包括字符串...
这本书旨在帮助开发者提升JavaScript技能,掌握DOM(Document Object Model)的精髓,从而实现更高效、更灵活的前端开发。 首先,JavaScript是一种广泛应用于网页和网络应用的脚本语言,由 Netscape 公司开发,最初...
《JavaScript编程实例五十讲》这本书无疑为初学者提供了宝贵的实践资源,同时也为有经验的程序员提供了回顾和深化理解的机会。以下将针对JavaScript的一些核心概念和常见实例进行详细阐述。 1. 变量与数据类型:...