Differences between jsquerySelector and getElementById using id to obtain elements _ javascript skills

Source: Internet
Author: User
This was discovered by sina's colleague xiaoniu. The difference between querySelector and getElementById in retrieving elements through id is found by sina's colleague xiaoniu, as follows:

The Code is as follows:









Script
Var str = '02e503e2a1c011cfc85b7b701a0677ec0900000000000001 ';
Function bySelector (id ){
Return document. querySelector ('#' + id );
}
Function byId (id ){
Return document. getElementById (id );
}

Alert (bySelector (str ));
Alert (byId (str ));
Script



The bySelector and byId functions obtain the element through querySelector and getElementById respectively.

The page contains the element "02e503e2a1c011cfc85b7b701a0677ec090000000001.

Result: none of the browsers that support querySelector can be obtained through bySelector (an error is reported), but can be obtained through getElementById.

It is suspected that the string of the id is too long, and the querySelector cannot be obtained. The real reason is that querySelector is implemented according to css specifications, that is, the css identifier cannot start with a number.

W3 writes
In CSS, identifiers (including element names, classes, and IDs in 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 (see next item ). for Instance, the identifier "B & W? "May 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.