Complete parsing of js selectors and comprehensive parsing of selectors

Source: Internet
Author: User

Complete parsing of js selectors and comprehensive parsing of selectors

The native JS selectors include getElementById, getElementsByName, getElementsByTagName, and getElementsByClassName. Next I will introduce the usage of these four selectors one by one.

1. getElementById (get the element through ID)

Usage: document. getElementById ("Id"); Id is the id attribute value of the element to be obtained.

2. getElementsByName (get the element through the name attribute)

Usage: document. getElementsByName ("Name"); Name is the name attribute value of the element to be obtained. This method is generally applicable to submitting form data, when the name attribute is set when the elements are form, img, iframe, applet, embed, and object, an attribute named after this name attribute value is automatically created in the Document object. Therefore, you can use document. domName to reference the corresponding dom object.

3. getElementsByTagName (obtain the element through the element name)

Usage: document. getElementsByTagName (TagName); TagName is the Tag Name of the element to be obtained. When TagName is *, all elements are obtained. You can also change the document to a DOM element, however, only the subset elements after the DOM element can be obtained.

4. getElementsByClassName (obtain elements through CSS classes)

Usage:Document. getElementsByClassName (ClassName); ClassName is the name of the CSS class for obtaining elements. If you want to obtain multiple CSS classes at the same time, separate them with spaces after each CSS class. Such as document. getElementsByClassName ("class2 class1") gets the elements of the class1 and class2 styles, and the document can also be changed to the DOM element. In this way, only the subset elements after the DOM element can be obtained.

<! DOCTYPE html> <! DOCTYPE html> 

The above is the js selector provided by the mini-editor to fully parse all the content. I hope you can provide more support to the customer's home ~

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.