关于javascript:JS常用积累

复制到剪贴板

function copyToClipboard(val) {
    let aux = document.createElement("input");
    aux.setAttribute("value", val);
    document.body.appendChild(aux);
    aux.select();
    document.execCommand("copy");
    document.body.removeChild(aux);
    showDefaultTips('已胜利复制到剪贴板','',1)
}

获取url参数

function getQueryVariable() {
    let query = window.location.search.substring(1);
    let vars = query.split("&");
    let res = {};
    for (let i = 0; i < vars.length; i++) {
        let pair = vars[i].split("=");
        if (pair[0] !== "") {
            let val = decodeURIComponent(pair[1]);
            if (val.includes("|")) {
                val = val
                    .split("|")
                    .filter((item) => item !== "")
                    .join(",");
            }
            if (val === "null") val = "";

            res[pair[0]] = val;
        }
    }
    return res;
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理