Home > Web Front-end > JS Tutorial > Summary of practical small functions in JavaScript (Collection)

Summary of practical small functions in JavaScript (Collection)

云罗郡主
Release: 2018-10-29 16:33:22
forward
2372 people have browsed it

What are the practical small functions in JavaScript? I believe that many friends who have just come into contact with JavaScript will have such questions. This chapter will introduce to you some practical JavaScript functions that have certain reference value. Friends in need can refer to them. I hope it will be helpful to you.

Summary of practical small functions in JavaScript (Collection)

1. Modify the get class method by yourself

// 获取class
<script type="text/javascript">
function getByClass(oParent,sClass){
        var aEle=oParent.getElementsByTagName("*");
        var aResult=[];
        for(var i=0,tt=aEle.length;i<tt;i++){ if(aele[i].classname.indexof(sclass)="">=0){
                        var arr_class=aEle[i].className.split(" ");
                        for(var j=0,len=arr_class.length;j<len;j++){ if(arr_class[j]="=sClass){" aresult.push(aele[i]);="" }="" return="" aresult;="" <="" script>="" <="" pre=""></len;j++){></tt;i++){>
Copy after login

For the above code, we can use the "HTML Online Editor" to test it.

2. Get element style

// 获取样式
function getStyle(obj,name){
    if(obj.currentStyle){
        return obj.currentStyle[name];
    }
    else{
        return getComputedStyle(obj,false)[name];
    }
}
Copy after login

3. Prevent event bubbling

// 阻止事件冒泡
function stopBubble(e){
        if(e && e.stopPropagation){
                e.stopPropagation();
        }
        else{
                window.event.cancelBubble=true;
        }
        return false;
}
Copy after login

4. Full screen mode

// 进入全屏
function fullScreen() {
        var el = document.documentElement;
        var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;
        if (typeof rfs != "undefined" && rfs) {
                rfs.call(el);
        }else if(typeof window.ActiveXObject != "undefined") {
                // for Internet Explorer
                var wscript = new ActiveXObject("WScript.Shell");
                if(wscript != null) {
                        wscript.SendKeys("{F11}");
                }
        }
}
// 退出全屏
function exitFullScreen() {
    var el = document,
    cfs = el.cancelFullScreen || el.webkitCancelFullScreen || el.mozCancelFullScreen || el.exitFullScreen,
    wscript;
    if (typeof cfs != "undefined" && cfs) {
      cfs.call(el);
      return;
    }
    if (typeof window.ActiveXObject != "undefined") {
        wscript = new ActiveXObject("WScript.Shell");
        if (wscript != null) {
            wscript.SendKeys("{F11}");
        }
          }
}
Copy after login

The above is a complete introduction to the summary of practical JavaScript functions. If you want to know more about JavaScript video tutorial, please pay attention to the PHP Chinese website.


The above is the detailed content of Summary of practical small functions in JavaScript (Collection). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:lvyestudy.com
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