Home Web Front-end HTML Tutorial DOM notes (4): HTML 5 DOM complex data type_html/css_WEB-ITnose

DOM notes (4): HTML 5 DOM complex data type_html/css_WEB-ITnose

Jun 24, 2016 am 11:52 AM

HTML 5 DOM defines complex data types such as collections and lists for convenient operations. Compared with HTML 4 DOM, HTML 5 DOM adds HTMLCollection, DOMTokenList, DOMStringMap, HTMLAllCollection, HTMLFormControlsCollection, etc.

1. HTMLCollection, HTMLAllCollection and HTMLFormControlsCollection

The three interfaces are used to represent a list composed of a set of elements. Both HTMLAllCollection and HTMLFormControlsCollection inherit from the HTMLCollection interface and rewrite the namedItem() method defined in the HTMLCollection interface. In DOM Notes (1): HTMLDocument interface, use document.forms (/images/embeds, etc.) to return an HTMLCollection object, document.all returns an HTMLAllCollection object, and document.forms.elements returns an HTMLFormControlsCollection object, which contains All form elements in the form.

Attribute or method Data type Description
length long Read-only, returns the number of elements in the collection. All three interfaces have this attribute
item() object 1. Get the elements in the collection according to the index. Define
属性或者方法 数据类型 说明
length long 只读,返回集合中的元素数量。三个接口均有此属性
item() object 1、根据索引获取集合中的元素。在HTMLCollection中定义

2、在HTMLCollection中有如下等价:
       document.forms(0)<=>document.forms[0]<=>document.forms.item(0)

tags() HTMLAllCollection 根据标签名获取元素。在HTMLAllCollection中定义
namedItem() object 根据name或者id属性获取集合中的元素,区别如下:

1、在HTMLCollection中,先根据name查找,在根据id查找,但是只返回第一个符合的元素。若不存在符合的元素则返回null。存在如下等价:
document.forms['formname']<=>document.forms(‘formname’)<=>document.forms.namedItem(‘formname’)

2、在HTMLAllCollection中,若有多个符合的元素,则返回一个HTMLCollection对象;若只有一个符合元素,则返回该元素。

3、在HTMLFormControlsCollection中,若有多个符合的元素,则返回一个RadioNodeList对象;若只有一个符合元素,则返回该元素。

in HTMLCollection 2. It has the following equivalent in HTMLCollection:
document.forms(0)<=>document.forms[0]<=>document.forms.item(0 )

tags() HTMLAllCollection Get elements based on tag names. Defined in HTMLAllCollection
namedItem() object Get the elements in the collection based on the name or id attribute. The differences are as follows: 1. In HTMLCollection, first search based on name, then search based on id, but only return the first matching element. If there is no matching element, null is returned. The following equivalents exist:
document.forms['formname']<=>document.forms('formname')<=>document.forms.namedItem('formname')

2. In HTMLAllCollection, if there are multiple matching elements, an HTMLCollection object is returned; if there is only one matching element, the element is returned.

3. In HTMLFormControlsCollection, if there are multiple matching elements, a RadioNodeList object is returned; if there is only one matching element, the element is returned.
属性或方法名 数据类型 说明
length long 可读写。若属性值大于实际的option元素个数,则HTMLSelectElement自动添加空的option元素;
若属性值小于实际的option元素个数,则HTMLSelectElement自动删除多余的option元素
add() void 插入一个option元素
namedItem() object 根据name或id获取集合中的元素
remove() void 删除一个option元素

2. HTMLOptionsCollection interface

This interface returns a list of elements. Different from HTMLCollection, It specifically returns a list of option elements and overrides the length attribute and namedItem() method. The data type returned by the HTMLSelectElement.options property is HTMLOptionsCollection.

属性或方法 数据类型 说明
length long 集合中标识的数量
value DOMString 获取或设置DOMTokenList值
add() void 插入一个标识
contains() boolean 判断是否包含某标识
remove() void 删除一个标识
item() object 根据索引获取标识
toggle() boolean 标识存在则删除,不存在则添加
toString() DOMString 返回对象的字符串形式
3. DOMTokenList and DOMSettableTokenList DOMTokenList represents a series of identifiers separated by spaces. In DOM Notes (3): In the Element interface and HTMLElement interface, the data type returned by the HTMLElement.classList property is DOMTokenList. DOMSettableTokenList inherits from DOMTokenList, the only difference is the addition of the value attribute. The data type returned by HTMLLinkElement.size is DOMSettableTokenList.

4. DOMStringMap and NodeList

The DOMStringMap interface marks a collection of "key/value pairs", and the key-value pairs can be deleted, modified, etc. through JavaScript syntax. In DOM Notes (3): Element interface and HTMLElement interface, the data type returned by the HTMLElement.dataset property is DOMStringMap

<html><head>     <title>DOMStringMap</title></head><body>     <img id="ex" data-ship-id="92432" data-x="30" data-y="10"           data-z="90"/>     <script>           var img = document.getElementById("ex");         //访问键值           alert(img.dataset.z);         //设置键值          img.dataset.shipId="343434";         //创建新键值对          img.dataset.defend=100;         //删除键值          delete img.dataset.y;     </script></body></html>
Copy after login

NodeList interface represents a list composed of a series of nodes. document.getElementsByTagName() returns a NodeList object. It has a read-only length property, which is used to return the number of nodes; and an item() method, which is used to access nodes based on index. There are the following equivalent methods:

nodelist.item(0) <=>nodelist[0]<=>nodelist(0)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

What is the difference between <strong>, <b> tags and <em>, <i> tags? What is the difference between <strong>, <b> tags and <em>, <i> tags? Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

See all articles