JS Queryselector and getElementById by ID to get the difference between elements _javascript tips

Source: Internet
Author: User

This is discovered by Sina colleague Xiaoniu, as follows

<! DOCTYPE html> 
 
 

Two function Byselector,byid get the element through Queryselector and getElementById respectively.

There is an element with the id "02e503e2a1c011cfc85b7b701a0677ec0900000000000001" on the page.

Result: All browsers that support Queryselector are not getting the Byselector (error), but can be obtained by getElementById.

A string that begins to suspect that the ID is too long causes queryselector to get it. The real reason is that Queryselector is implemented by CSS specification, that is, CSS identifiers cannot begin with numbers.

W3 writes
in CSS, identifiers (including element names, classes, and IDs into selectors) can contain only the characters [a-za-z0-9] and ISO 10646 characters u+00a0 and higher, plus the hyphen (-) and the underscore (_); They cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (the next item). For instance, the identifier "B&w?" could be written as "b\&w\" or "b\26 w\3f".

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.