Resolves an issue where IE8 (and below) does not support Getelementsbyclassname

Source: Internet
Author: User

This is my 1th technical note, to share with you: Solve IE8 (and below) does not support the Getelementsbyclassname method. If there is a wrong place, please tell me the message, I corrected in time, one is to update my cognition, and secondly also do not mislead others, thank you.

When we set the style of the element, we can say that the most used is the class. Unfortunately, in the low version of IE (IE8 and below), we cannot use getelementsbyclassname because IE does not add this method to the Document object.

This result is false in IE8 and in the following versions
Console.info (' Getelementsbyclassname ' in window.document);

Below, share my getElementsByTagName's compatible wording:

    /*parameter description: obj parameter: A DOM object, the GetClass function obtains all the element nodes under this object.    Sclass: A string that gets the class name of the element. */    functionGetClass (obj,sclass) {//prepares an array to be returned as a result of a function call        varAresult = []; //If there is getelementsbyclassname in the Docuemnt object, we will use the browser to provide
Because the browser provides more efficiency if(' Getelementsbyclassname 'inchdocument) {Aresult=Obj.getelementsbyclassname (Sclass); } //If the browser does not then we have to achieve a total of 4 steps Else{ //1. Get all the elements under obj varAele = Obj.getelementsbytagname (' * ')); //2. Check each child element with a for loop for(vari=0; i<aele.length; i++ ){ //3. Get each child element classname value This value is a string varstr =Aele[i].classname; //4. If the class attribute of the element has the class we need, then put it in Aresult. if(Str.indexof (Sclass)!=-1) {Aresult.push (aele[i]); } } } returnAresult; }

Resolves an issue where IE8 (and below) does not support Getelementsbyclassname

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.