How to get nodes by using ID in JavaScript

Source: Internet
Author: User

In current webpage development, the element ID attribute may only be used for searching elements in JavaScript, because it cannot be used repeatedly and violates the CSS coding principles, however, we have recently seen some alternative use.

When learning JavaScript, the method of getting node objects by ID is as follows:

Var idnode = document. getElementById ("idname ")

Then, you can obtain the idnode to change the style:

Idnode. style. color = "# ff0"

But by chance, someone wrote this:

Idname. style. color

My first thought is that writing in this way is definitely wrong. However, the same result as the first method can be obtained! It's too unscientific. If we can get node objects in this simple way, isn't the first method eliminated?

Then I checked some information. The second method is only available in some high-version browsers, with poor compatibility. And does not comply with JS syntax specifications. You can use it for a simple demo. For formal development, use the first method.

Note: getElementById obtains an Object set, that is, [Object].

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.