The difference between the name value and the ID value when the JS gets the element

Source: Internet
Author: User

1. Scope of application

The ID can be used except base, head, HTML, script, meta, title tag, and name only applies to select, form, Frame, iframe, IMG, a, input, and so on.

HTML element Input type= ' radio ' grouping, only one radio can be selected at the same time, this grouping is implemented according to the same name property.

2. How to obtain

Get Id:document.getElementById ("id");

Get Name:document.getElementsByName ("name"); note: Because the name value is not unique, the resulting result is a number of groups;

3. Access nodes in the DOM tree using Document.ID and Document.name

Using Document.ID and Document.name to access the nodes in the DOM tree, you can access the IMG element, the IFRAME element, and the form element. However, this approach is not specified in the Web Standard, so support for these methods varies by browser.

Instance:

Use the Document.name property directly to modify the property values of the IMG tag

<HTML><Head><Scripttype= "Text/javascript">functionMouseOver ()
{DOCUMENT.B1.SRC="/i/eg_mouse.jpg"}functionMouseOut () {document.b1.src="/i/eg_mouse2.jpg"}</Script></Head><Body><ahref= "/index.html"Target= "_blank"><imgBorder= "0"alt= "Visit w3school!"src= "/i/eg_mouse2.jpg"name= "B1"onmouseover= "MouseOver ()"
onmouseout= "MouseOut ()" /></a></Body></HTML>

The difference between the name value and the ID value when the JS gets the element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.