The things about the JS Package Framework class Library (i)

Source: Internet
Author: User
Tags terminates

    • The traditional approach to selector modules
        var function (tag) {            return  document.getelementsbytagname (tag);        }         var function (ID) {            return  document.getElementById (ID);        }         var function (className) {            return  document.getelementsbyclassname (className);        }    
    • The method of tag

This is the HTML part of the code

<! DOCTYPE html>            Div, p {                width:200px;                 height:50px;                 border:1px dashed red;                  0;            }         </style>    
View Code
//For example, to add a background color to a div box, we might do this.        varDIVs = tag (' div '); vareach =function(arr) { for(vari = 0;i< arr.length;i++) {Arr[i].style.backgroundcolor= ' Pink ';        }} each (divs); //it might be.        varList = tag (' div '); vareach =function(ARR,FN) { for(vari = 0;i<arr.length;i++){                if(Fn.call (arr[i],i,arr[i]) = = =false){//is equal to false, terminates the loop                     Break; }}} each (list,function(){             This. Style.backgroundcolor = ' Yellow '; })
//Fn.call (arr[i],i,arr[i]) = = = False, the following function also uses this method of invocation, you can do a comparison        vararr = [1, 2, 3, 4, 3, 5]; vareach =function(arr, fn) { for(vari = 0; i < arr.length; i++ ) {                if(FN (i, arr[i]) = = =false ) {                     Break;                }            }        }; varindex =-1; Each (arr,function(i, v) {if(v = = 3) {Index=i; return false;//If the break traversal does not terminate until the traversal is completed            }        } ); Console.log (index);//2
View Code

/*var tag = function (tag) {
return document.getElementsByTagName (tag);
}*/
The disadvantage of this type of writing is that every time you get an element, you return it, which results in a loss of browser performance.
To simplify development, you can merge the obtained array into an array, which is also used in the JQ

varGettag =function(tag, results) {results= Results | | [];            Results.push.apply (results, document.getelementsbytagname (tag)); returnresults;        }; varList = Gettag (' div '); List= Gettag (' p ', list);//if you get the P tag again, you can write this.        vareach =function(ARR,FN) { for(vari = 0;i<arr.length;i++){                if(Fn.call (arr[i],i,arr[i]) = = =false){//is equal to false, terminates the loop                     Break; }            }        }        /*Each (List,function () {this.style.backgroundColor = ' lightgreen '; })*/Each (Gettag (' P ', Gettag (' div '),function(){             This. Style.backgroundcolor = ' LightGreen '; })

About the JS Encapsulation Framework Class Library (a)

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.