零碎方法笔记

网页icon

<link rel="shortcut icon" href="images/dnalims.ico">

网页可视高度 

document.documentElement.clientHeight

获取某个元素的实际位置

var rect = Dom.getBoundingClientRect();

单个或多个的iframe高度自适应

//根据窗口大小,页面自适应高度
        window.onresize = function () {
            changeFrameHeight();
        }
        function changeFrameHeight(sideno) {
            var client_height = document.documentElement.clientHeight
            // 如果有索引(单个),仅按索引修改
            if (sideno != undefined && sideno != null && sideno != "") {
                var ifm = document.querySelector("#MainBox iframe.ifm_" + sideno)
                ifm.height = client_height - 56
            }
            // 如果没有索引(多个),全部修改
            else {
                var ifms = document.querySelectorAll("#MainBox iframe")
                for (var i = 0; i < ifms.length; i++) {
                    ifms[i].height = client_height - 56
                }
            }
        }

简单的原生XHR请求

//获取登录信息
function getLoginInfo() {
    var xmlhttp = new XMLHttpRequest()
    xmlhttp.open("post", "/api/getuser", false);
    //post请求设置为表单格式
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("{}");//里面一定是字符串!!!因为Http协议通信的内容全是字符串格式
    if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
        var res = xmlhttp.responseText
        //xmlhttp.responseText是返回的结果
        if (res == "error: no login user") {
            exitSystem()
        } else {
            res = JSON.parse(res)
            //head_info.placeName = res.organization_name
            head_info.userName = res.full_name
            head_info.user_id = res.id
        }
    }
}

 元素高亮

//为选中的一级导航项目添加高亮样式
function addHighLight(sideno) {
    var link_obj_arr = document.querySelectorAll('.link')
    for (var i = 0; i < link_obj_arr.length; i++) {
        //设置选中的.link元素的curritem
        if (link_obj_arr[i].getAttribute('init-no') == sideno) {
            link_obj_arr[i].classList.add('curritem');
        }
        //移除非选中的.link元素的curritem
        else {
            link_obj_arr[i].classList.remove('curritem');
        }
    }
}

 js时间戳转2017-01-01 00:00:00格式

function add0(m) {
    return m < 10 ? '0' + m : m
}
function format(shijianchuo) {
    //shijianchuo是整数,否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

获取当前时间(yyyy-mm-dd hh:mm:ss)

function formatTen(num) {
            return num > 9 ? (num + "") : ("0" + num);
        }
        // elem ui中国标准时间--转换为日期'1900-01-01 00:00:00'格式
        function getNowDateTime(time) {
            let date = new Date()
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDate();
            let hour = date.getHours();
            let minute = date.getMinutes();
            let second = date.getSeconds();
            return year + "-" + formatTen(month) + "-" + formatTen(day) + " " + formatTen(hour) + ":" + formatTen(minute) + ":" + formatTen(second);
        }
        console.log(getNowDateTime())

移除元素下的所有子元素

 function removeAllChild(curr_content_obj) {
     while (curr_content_obj.hasChildNodes()) //当div下还存在子节点时 循环继续
     {
         curr_content_obj.removeChild(curr_content_obj.firstChild);
     }
 }

js判断浏览器版本  (原文:https://blog.csdn.net/u012767607/article/details/54563498

function getExplorerInfo() {
        var explorer = window.navigator.userAgent.toLowerCase();
        //ie 
        if (explorer.indexOf("msie") >= 0) {
            var ver = explorer.match(/msie ([d.]+)/)[1];
            return { type: "IE", version: ver };
        }
        //firefox 
        else if (explorer.indexOf("firefox") >= 0) {
            var ver = explorer.match(/firefox/([d.]+)/)[1];
            return { type: "Firefox", version: ver };
        }
        //Chrome
        else if (explorer.indexOf("chrome") >= 0) {
            var ver = explorer.match(/chrome/([d.]+)/)[1];
            return { type: "Chrome", version: ver };
        }
        //Opera
        else if (explorer.indexOf("opera") >= 0) {
            var ver = explorer.match(/opera.([d.]+)/)[1];
            return { type: "Opera", version: ver };
        }
        //Safari
        else if (explorer.indexOf("Safari") >= 0) {
            var ver = explorer.match(/version/([d.]+)/)[1];
            return { type: "Safari", version: ver };
        }
    }
    alert("浏览器:" + getExplorerInfo().type + "
 版本:" + getExplorerInfo().version);
原文地址:https://www.cnblogs.com/sangzs/p/8558680.html