Htmlcollection and nodelist

Source: Internet
Author: User

The following method gets the htmlcollection object

 
Document. images // All IMG elements document. links // all elements a and area with the href attribute document. anchors // all elements a with the name attribute document. forms // All form elements document. scripts // All script elements document. applets // All applet elements document. embeds // All embed elements document. plugins // document. same as embeds document. getelementbyid ("table "). childrendocument. getelementbyid ("table "). tbodiesdocument. getelementbyid ("table "). rowsdocument. getelementbyid ("row "). cellsdocument. getelementbyid ("map "). areasdocument. all // htmlallcollection, similar to htmlcollection document. getelementbyid ("F2 "). elements // htmlformcontrolscollection extends htmlcollectiondocument. getelementbyid ("S "). options // htmloptionscollection extends htmlcollection

The following method gets the nodelist object

Document. getelementsbyname ("bing") document. getelementsbyclassname ("no") document. getelementsbytagname ("A") document. getelementsbytagnamens ("*", "A") document. queryselectorall ("A") document. getelementbyid ("table "). childnodesdocument. stylesheets // stylesheetlist, similar to nodelist

I. Similarities

Htmlcollection and nodelist have many similarities.

1. All are class array objects

All have the Length attribute and can be iterated through the for loop.

2. Read-Only

3. All are real-time, that is, changes to the document will be immediately reflected on the relevant objects.

 
VaR F = document. forms; console. log (F. length); // 2var temp = document. createelement ("form"); document. body. appendchild (temp); console. log (F. length); // 3

One exception is that the nodelist returned by document. queryselectorall is not real-time.

4. The item () method is available.

You can use item (INDEX) or Item ("ID") to obtain elements.

5. You can access elements through attributes, such as document. Forms. F1 (F1 is the form ID)

Ii. Differences

1. The htmlcollection object has the nameditem () method. You can pass the ID or name to obtain the element.

2. The item () method of htmlcollection and the element (document. Forms. F1) obtained through attributes can support ID and name, while the nodelist object only supports ID

Appendix: ExampleCodeHtml

<! Doctype HTML> 

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.