JavaScript常用事件

键盘enter键

$(document).keypress(function (e) {
            // 回车键事件  
            if (e.which == 13) {
                $('input[type="button"]').click();
            }
        });

显示全屏

var fullscreen = function () {
            elem = document.body;
            if (elem.webkitRequestFullScreen) {
                elem.webkitRequestFullScreen();
            } else if (elem.mozRequestFullScreen) {
                elem.mozRequestFullScreen();
            } else if (elem.requestFullScreen) {
                elem.requestFullscreen();
            } else {
                //浏览器不支持全屏API或已被禁用  
            }
        }  

 select全选/非全选

$(".checkall").click(function () {
        $(".checklist").prop("checked", this.checked);
    });

 格式化日期

<!--格式化字符串时间格式为yyyy-mm-dd-->
    function formatDate(date) {
        let d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear();

        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;

        return [year, month, day].join('-');
    }

 根据单选框,改变值

$(document).ready(function () {
        $('input[type=radio][name=optionsRadios]').change(function () {
            if (this.value == 'invoice_type_com') {
                document.getElementById("invoice_type").value = 1300163160;
            } else if (this.value == 'invoice_type_pro') {
                document.getElementById("invoice_type").value = 1300173160;
            }
        });
    });

根据input框的值变化触发事件

$(function(){
        $('#bef_product_price').bind('input propertychange', function() {
            cal_tax_price();
        });
        $('#product_num').bind('input propertychange', function() {
            cal_tax_price();
        });

 操作数组中的对象

// 判断对象的值与传入值是否相等
function isObjectValueEqual(a, b) {
        if(typeof(a) != "object" && typeof(b) != "object"){
            if(a == b){
                return true;
            }else{
                return false;
            }
        }
        var aProps = Object.getOwnPropertyNames(a);
        var bProps = Object.getOwnPropertyNames(b);

        if (aProps.length != bProps.length) {
            return false;
        }

        for (var i = 0; i < aProps.length; i++) {
            var propName = aProps[i];

            if (a[propName] !== b[propName]) {
                return false;
            }
        }

        return true;
    };


// 获取数组中对象的索引
function getIndexWithArr(_arr,_obj) {
        var len = _arr.length;
        for(var i = 0; i < len; i++)
        {
            if(isObjectValueEqual(_arr[i],_obj)) {
                return i;
            }
        }
        return -1;
    };



// 删除数组中的对象
function removeObjWithArr(_arr,_obj) {
        var length = _arr.length;
        for(var i = 0; i < length; i++)
        {
            if(isObjectValueEqual(_arr[i],_obj))
            {
                if(i == 0)
                {
                    _arr.shift(); //删除并返回数组的第一个元素
                    return;
                }
                else if(i == length-1)
                {
                    _arr.pop();  //删除并返回数组的最后一个元素
                    return;
                }
                else
                {
                    _arr.splice(i,1); //删除下标为i的元素
                    return;
                }
            }
        }
    };
原文地址:https://www.cnblogs.com/jiaqi-666/p/9767962.html