Table of Contents
Example Heading
DHTML Fun!!!
Home Web Front-end JS Tutorial document.all and WEB standards_Basic knowledge

document.all and WEB standards_Basic knowledge

May 16, 2016 pm 07:24 PM

1. DOM

WEB standards are really popular now, but what is discussed below is a document.all[] that does not meet the standards. DOM - DOCUMENT OBJECT MODEL document object model provides methods to access document objects. For example, if there is a table in the document and you want to change its background color, you can use document.all[] to access the TABLE in javascript. But the DOM is also different. Because of the competition between browser manufacturers, each browser manufacturer has developed its own private DOM, which can only run correctly on its own browser. document.all[] can only run on IE. Microsoft's private DOM. In order to correctly understand the DOM, the DOM of IE4 is given

2. Understand document.all[]

Starting from IE4, IE’s object model only added document.all[ ], let’s take a look at the Description of document.all[]:
Array of all HTML tags in the document.Collection of all elements contained by the object.

That is to say, document.all[] is a document An array variable composed of all tags in , including all elements in the document object (see Example 1).

IE's document.all collection exposes all document elements. This array provides access to every element in the document.

document.all[] This array provides access to all elements in the document.

Example 1 (this allows you to understand which objects are in the document)


[Ctrl A to select all Note: If you need to introduce external Js, you need to refresh to execute
]
  Example 2 (Accessing a specific element)


[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]


The above example lets you understand how to access a specific element in the document. For example, if there is a DIV in the document
, you can use this Access this DIV using the ID, NAME or INDEX attribute of the DIV:
Copy the code
The code is as follows:

document.all["docid"]
document.all["docname"]
document.all.item("docid")
document.all.item("docname")
document. all[7] document.all.tags("div") returns an array of all DIVs in the document. In this example, there is only one DIV, so you can access it with document.all.tags("div")[0] .
  3、使用document.all[]

例3


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

  4、标准DOM中的访问方法

  开头就说过document.all[]不符合WEB标准,那用什么来替代它呢?document.getElementById


Most third-party browsers are â€œstrict standards” implementations, meaning that they implement W3C and ECMA standards and ignore most of the proprietary object models of Internet Explorer and Netscape.If the demographic for your Web site includes users likely to use less common browsers, such as Linux aficionados, it might be a good idea to avoid IE-specific features and use the W3C DOM instead. by Internet Explorer 6, we see that IE implements significant portions of the W3C DOM.

  这段话的意思是大多数第三方浏览器只支持W3C的DOM,如果你的网站用户使用其他的浏览器,那么你最好避免使用IE的私有属性。而且IE6也开始支持W3C DOM。

  毕竟大多数人还不了解标准,在使用标准前,你还可以在你的网页中用document.all[]访问文档对象前面写到WEB标准,今天继续WEB标准下可以通过getElementById(), getElementsByName(), and getElementsByTagName()访问DOCUMENT中的任一个标签:

  1、getElementById()

  getElementById()可以访问DOCUMENT中的某一特定元素,顾名思义,就是通过ID来取得元素,所以只能访问设置了ID的元素。

  比如说有一个DIV的ID为docid:



  那么就可以用getElementById("docid")来获得这个元素。


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

  2、getElementsByName()

  这个是通过NAME来获得元素,但不知大家注意没有,这个是GET ELEMENTS,复数ELEMENTS代表获得的不是一个元素,为什么呢?

  因为DOCUMENT中每一个元素的ID是唯一的,但NAME却可以重复。打个比喻就像人的身份证号是唯一的(理论上,虽然现实中有重复),但名字重复的却很多。如果一个文档中有两个以上的标签NAME相同,那么getElementsByName()就可以取得这些元素组成一个数组。

  比如有两个DIV:




  那么可以用getElementsByName("docname")获得这两个DIV,用getElementsByName("docname")[0]访问第一个DIV,用getElementsByName("docname")[1]访问第二个DIV。

  下面这段话有错,请看forfor的回复,但是很可惜,IE没有支持这个方法,大家有兴趣可以在FIREFOX或NETSCAPE中调试下面这个例子。(我在NETSCAPE7.2英文版和FIREFOX1.0中调试成功。)


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

  看来最新版浏览器理解WEB标准还是有问题,我知道的只有盒模型、空格BUG、漂浮BUG、FLASH插入BUG,从document.getElementsByName可以看出FIREFOX,NETSCAPE理解标准有偏差,但forfor说的对:要灵活应用标准。

  3、getElementsByTagName()

  这个呢就是通过TAGNAME(标签名称)来获得元素,一个DOCUMENT中当然会有相同的标签,所以这个方法也是取得一个数组。

  下面这个例子有两个DIV,可以用getElementsByTagName("div")来访问它们,用getElementsByTagName("div")[0]访问第一个DIV,用

getElementsByTagName("div")[1]访问第二个DIV。


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
  总结一下标准DOM,访问某一特定元素尽量用标准的getElementById(),访问标签用标准的getElementByTagName(),但IE不支持getElementsByName(),所以就要避免使用getElementsByName(),但getElementsByName()和不符合标准的document.all[]也不是全无是处,它们有自己的方便之处,用不用那就看网站的用户使用什么浏览器,由你自己决定了。

  关于document.getElementsByName

  IE当然支持 å¯ä»¥è¯´IE更忠于html/xhtml标准(嘿嘿 åŽŸæ¥firefox也不咋地 å¹¸ç¾ä¹ç¥¸ä¸€ä¸‹^_^)

  按照O'REILLY的<>中的说法 name并不是核心属性 å¹¶éžæ‰€æœ‰æ ‡ç­¾éƒ½å¯ä»¥åŠ name属性(大家可以拿我下面的例子去 validator.w3.org åšéªŒè¯)

  所以你给div加name属性理论上是不会出结果的.这一点IE很好的符合了标准~!!

  (同时也看出了符合标准也有烦人的地方~_~ æ‰€ä»¥å¤§å®¶ä¸ç”¨å¤ªæŠŠæ ‡å‡†å½“回事儿 è¿‡ä¸¤å¹´éƒ½ç”¨xml了 è¿™ä¸ªä¹Ÿè¿‡æ—¶äº†!倡导灵活的webstandard应用思想 é™¤äº†ç¬¦åˆxml思想的东西 å…¶ä»–的按浏览器的理解去做就行)

附: 


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]


  简单来说就是DIV不支持NAME属性,所以那个document.getElementsByName的例子调试不能通过.
下面用INPUT做个例子

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Can PowerPoint run JavaScript? Can PowerPoint run JavaScript? Apr 01, 2025 pm 05:17 PM

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

See all articles