Native JavaScript implementations get all descendant element code instances under the specified element

Source: Internet
Author: User

Native JavaScript implementations get all descendant element code instances under the specified element:
The code that implements this feature is described in this section, but it is cumbersome in terms of code volume and is used in the form of recursive recursion.
The way you say it can refer to native JavaScript gets all the elements under the specified element node section, this article looks very troublesome, then share a relatively simple way, the use of native JavaScript method to achieve this function.
The code example is as follows:

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "Utf-8">5 <Metaname= "Author"content= "http://www.softwhy.com/" />6 <title>Ant Tribe</title>7 <Scripttype= "Text/javascript">8 window.onload=function(){9   varObox=document.getElementById ("Box");Ten   varOshow=document.getElementById ("Show"); One   varnodes=Obox.getelementsbytagname ("*"); A oshow.innerhtml=nodes.length; - } - </Script> the </Head> - <Body> - <DivID= "Show"></Div> - <DivID= "box"> +   <Div> -     <ul> +       <Li>Ant Tribe One</Li> A       <Li>Ant Tribe II</Li> at       <Li>Ant Tribe Three</Li> -     </ul> -   </Div> - </Div> - </Body> - </HTML>

The above code implements our requirements, and the parameter asterisk denotes a wildcard character that can match all types of labels.
The invocation object of the getElementsByTagName () method determines its lookup scope.

Native JavaScript implementations get all descendant element code instances under the specified element

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.