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

Summary of practical small functions in JavaScript (Collection)

云罗郡主
云罗郡主forward
2018-10-29 16:33:222369browse

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++){>

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

3. Prevent event bubbling

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

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

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!

Statement:
This article is reproduced at:lvyestudy.com. If there is any infringement, please contact admin@php.cn delete