Daily Learning notes----JS can be called directly with the ID name of the problem?

Source: Internet
Author: User

In JavaScript, the standard ID selector invocation syntax is:

document.getElementById (' myID '). Style.width = pc + "%";

However, today, it is found that directly with the ID name call unexpectedly can also run correctly:

myid.style.width = pc+ "%";

Finally looked for Niang asked.

See MRCXT's blog has this problem, the answer to the question is:

It was originally IE inside, and it seemed to support it later firefox chrome . Not recommended, this is not the standard inside, not necessarily support in the future. And the code is easy to write confusing, belong to the multiNavItem1 global scope, and you can assign to him, after the assignment is the new value, the value is not assigned to that element, when some of the id value is not, then some is this DOM object, some not, especially easy to confuse.

Looked for again, did not find the official answer, but should be similar to this.

So, the final conclusion is: can use, but is not the standard recommended, and error-prone, so it is not recommended to use!

Insight learn shallow, welcome to shoot Bricks!

Daily Learning notes----JS can be called directly with the ID name of the problem?

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.