javascript--write Var but can also use tags

Source: Internet
Author: User
Tags tag name

One problem with missing VAR:

This is not a standard notation! Do not recommend using

But the effect is still out, why?

Reason:

Https://zhidao.baidu.com/question/1637589020484843580.html

Why the object can be accessed directly by ID, because all element IDs are equivalent to a global variable that holds the object.

There are compatibility issues with direct access to objects through IDs.

It is recommended to get the object in the form of document.getElementById standard.

If an element conforms to either of the two rules below, the Window object must have an attribute corresponding to it, and the property value is the object.

If an element has an id attribute, the property value of the id attribute becomes the property name of the Window object. If an element has the Name property, the property value of the Name property becomes the property name of the Window object. But the tag name for this element must be: A, applet, area, embed, form, frame, frameset, iframe, IMG, Object, one of them.

But if the page has two elements of the same ID, it's a little different:

123<script type= "Text/javascript" >4         functionGetInput () {5             varMydiv = document.getElementById ("Div1");6alert (mydiv.length);//Mydiv is disphtmlelement .7alert (div1.length);//Div1 is disphtmlelementcollection .8         }9</script>Ten One<body> A<div id= "Div1" >123</div> -<div id= "Div1" >456</div> -<input type= "button" value= "Test" onclick= "GetInput ()"/> the</body> -

javascript--write Var but can also use tags

Related Article

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.