- 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); }
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)