Home > Web Front-end > JS Tutorial > JavaScript practical small functions (code examples)

JavaScript practical small functions (code examples)

云罗郡主
Release: 2018-11-10 15:22:20
forward
2284 people have browsed it

The content this article brings to you is about JavaScript practical functions, which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

  1. My own modified method of getting class

// 获取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

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 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 the complete introduction to JavaScript’s practical 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 JavaScript practical small functions (code examples). 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