Convert pseudo-arrays to arrays using the Apply method

Source: Internet
Author: User
Tags tagname

JS in a lot of DOM methods are for the array, but sometimes we will get some pseudo-array, at this time to manipulate the pseudo-array, then we must transform the pseudo-array into arrays, the following through the method of apply () demo An example, for your reference (the following is part of the code):

</Head><Body><span>Span</span> <span>Span</span><P>Ppp</P><span>Span</span> <span>Span</span><P>Ppp</P><span>Span</span> <span>Span</span><P>Ppp</P><Script>    functionT (tagname,context) {context=Context||document; returnContext.getelementsbytagname (tagName); }   varspans=T ('span' ); Console.log (spans);//Pseudo-Array    vararr=[]; //declares a new arrayarr.push.apply (Arr,spans); //The second argument using apply is a method of an array (a pseudo-array) that converts it into a group.Console.log (arr);//ArrayArr.foreach (function(v,i) {V.style.border='1px solid Red'; //a simple way to add a style is to use an array, etc.    })</Script>

The result is:

Convert pseudo-arrays to arrays using the Apply method

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.