Home > Web Front-end > JS Tutorial > JavaScript function encapsulation explanation

JavaScript function encapsulation explanation

巴扎黑
Release: 2017-07-24 17:03:30
Original
1760 people have browsed it


The function that js first needs
window.onload = function() {
   
    function $(id) {
       
          return document.getElementById(id);
       
  }

function $arr(array) {
return document.getElementsByTagName(array);
}
function hide(id) {
$( ID) .style.display = "None";

}
## Function Show (ID) {
$ (id) .style.display = "block";

}

}
Script loaded function
window.onload = function() {

}
2. Encapsulated id function
function $( id) {

return document.getElementById(id);

}
Call id $("called id")

3. Improve user experience—— The text box automatically gains focus 38

$("txt").focus();

Text box< input type = "text"id = "txt" / >
4. Improve the user experience - you can select all the text boxes with the mouse, making it easy to delete text
$("sele").onmouseover = function() {

this.select();

}
Text box< input type = "text"id = "sele" / >
5. Encapsulated array id
function $arr(array) {

return document.getElementsByTagName(array);

}
Call $arr("The called array such as li i tag")
6. Custom average function
function avg(array) {

var sum = 0;

var len = array.length;

for (var i = 0; i < len; i++) {
var len = array.length;
= array[i];
             
     
       
    return sum / len;
       
}

   
  for (var i = 0; i < inputs.length; i++) {
                inputs[i].checked = flag; , IE9 or above, including IE9), supports Document.GetelementsByClassName () functions, IE678 does not support]
Function Getclass (ClassName, ID) {
// The mainstream browser with ID
iF (Document.get.get.get ElementsByClassName)
                                                                                                                                                                                 
Return document.GetelementsByClassName (ClassName);
}
}
// The most 哏 哏 哏 哏 e i i
counter {
Var Eleid = TByid ( id);
              var dom=eleId.getElementsByTagName("*");              var dom=document.getElementsByTagName("*");
}
            var arr=[];
            for(var i=0;i                 var txtarr=dom[i].className.split(" ");
for(var j=0;j                      if(txtarr.className == classname){
                            arr.push(dom[j]);
                     }
                    }
               }
           }

9.隐藏显示函数
            function hide(id) {
                $(id).style.display = "none";
            }
            function show(id) {
                $(id).style.display = "block";
            }

The above is the detailed content of JavaScript function encapsulation explanation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template