造轮子之--JS/jQuery

1、判断数组中是否包含指定元素

[arr].contains([i])

    Array.prototype.contains = function (obj) {
            var i = this.length;
            while (i--) {
                if (this[i] == obj) {
                    return true;
                }
            }
            return false;
        }

2、蒙版

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>蒙板</title>
    <style>
        .container {
             900px;
            margin: 50px auto;
            text-align: center;
        }

        #myModel {
            position: absolute;
            top: 0;
            left: 0;
            display: none;
            background-color: rgba(9, 9, 9, 0.63);
             100%;
            height: 100%;
            z-index: 1000;
        }

        .model-content {
             50%;
            text-align: center;
            background: #ffffff;
            border-radius: 6px;
            margin: 100px auto;
            line-height: 30px;
            z-index: 10001;
        }
    </style>
</head>
<body>
<div class="container">
    <button onclick="showModel()">弹出蒙板</button>
    <div id="myModel" onclick="closeModel()">
        <div class="model-content">
            <p>你好啊,我是内容~~</p>
            <p>
                <button id="closeModel" onclick="closeModel()">关闭</button>
            </p>
        </div>
    </div>
</div>
<script>
    function showModel() {
        document.getElementById('myModel').style.display = 'block';
    }
    function closeModel() {
        document.getElementById('myModel').style.display = 'none';
    }
</script>
</body>
</html>

3、子页面关闭后自动刷新父页面

opener.location.reload()

4、获取所有选中的CheckBox的Name值

$('input:checkbox[name=check_content]:checked')

5、AngularJS 获取Scope

var scope = $('div[ng-controller="precision-controller"]').scope();

6、获取Url中Request参数

        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return unescape(r[2]);
            }
            return null;
        } 

$.getUrlParam(name)

7、时间字符转换函数

        Date.prototype.format = function (format) {
            var o = {
                "M+": this.getMonth() + 1, //month 
                "d+": this.getDate(), //day 
                "h+": this.getHours(), //hour 
                "m+": this.getMinutes(), //minute 
                "s+": this.getSeconds(), //second 
                "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
                "S": this.getMilliseconds() //millisecond 
            }

            if (/(y+)/.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }

            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                }
            }
            return format;
        }

 [date].format("yyyy-MM-dd hh:mm:ss")

8、跟随滚动条固定顶部导航

    //header 为导航上部元素
    //nav    为导航元素
    //main   为下部主体内容元素

    <script type="text/javascript">
        var i = 0;
        $(window).scroll(function () {
            var s = $(window).scrollTop();
            var headerHeight = $("#header").height();
            var indexHeight = $(".nav").height();
            if (s > headerHeight) {
                $(".nav").css({ "position": "absolute", "top": s, "width": "100%", "z-index": 999 });
                if (i == 0) {
                    $("#main").css({ "position": "relative", "top": indexHeight, "width": "100%" });
                    i = 1;
                }
            }
            else {
                $(".nav").css({ "position": "relative", "top": "0px" });
                $("#main").css({ "position": "relative", "top": "0px" });
                i = 0;
            }
        });
    </script>
原文地址:https://www.cnblogs.com/grpxiaoyu/p/7133345.html