Home > Web Front-end > JS Tutorial > When to use document and document.all respectively_JavaScript

When to use document and document.all respectively_JavaScript

WBOY
Release: 2016-05-16 19:29:25
Original
1025 people have browsed it
<body>
<div id="aa">123456 div>
<input type= "button" value="It's wrong to use document here" onclick="alert(document.aa.innerText);" />
<br />
<input type="button" value="It won’t go wrong if you use document.all here" onclick="alert(document.all.aa.innerText);" />
body>
Run the above code.

If the name attribute is set in the html tag corresponding to a, form object, image object, applet object, its value will be used as the attribute name of the document object to refer to the corresponding object and other objects. It is not possible.
In addition, if input is used as a sub-element of form, it is wrong to directly use inputName or document.inputName to reference this object. You must use formName.inputName to reference, otherwise you can use inputName to reference.
In addition, you should Notice that there are many commonly used elements without names.
If you want to reference an element with an id, you can only use Id or document.getElementById, document.all.id to reference
But for elements like this, so
like ...... can be used
linkid.href ;
linkname.href;
document.all.linkid.href;
document.all.linkname.href;
document.getElementById("linkid").href;
document.getElementsByName ("linkname")[0].href to reference

all is a collection that contains all html objects. Write a program to access all objects. Like this:

Be careful to put the program after .

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template