总结前端JQ常用的一些操作手法(慢慢完善)

时间:2021-05-29 11:24:58

1、实例化Js一个object对象,把它当做类来用,事例是操作url的参数

  function GetRequestCondition() {
            var url = window.location.href;
            var conditon = url.substring(url.indexOf("?") + 1, url.length);
            var strs = conditon.split("&");
            var theRequest = new Object();
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);  //注意需要用decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码。
            }
            return theRequest;
        }

        var Request = new Object();
        Request = GetRequestCondition();
        $("#OrderID").val(Request["OrderID"]);
        $("#TraderName").val(Request["TraderName"]);
        $("#MallName").val(Request["MallName"]);
        $("#Contacts").val(Request["Contacts"]);

2、替换URL中指定参数的值的方法

  function replaceParamVal(oldUrl, paramName, value) {
            var reg = eval('/(' + paramName + '=)([^&]*)/gi');
            var newUrl = oldUrl.replace(reg, paramName + '=' + value);return newUrl;
        }

3、前端js校验图片格式

     //校验图片格式
        function validateImgFormat() {
            var pic = document.getElementById("MallImage").value;
            if (pic == "") return true;
            var pos = pic.lastIndexOf(".");
            var lastname = pic.substring(pos, pic.length);
            var resultName = lastname.toLowerCase();
            if (resultName != ".jpg" && resultName != ".png" && resultName != ".bmp" && resultName != ".ico") {
                $.messager.alert('错误提示', "请上传jpg/png/ico/bmp格式的图片!", 'error');
                return false;
            }
            return true;
        }

4、复制页面链接(自己修改一下可以复制内容)

 //方法一
                function copyToClipBoard() {

                    if (window.clipboardData) {
                         window.clipboardData.clearData();
                        var clipBoardContent = "";
                        clipBoardContent += document.title;
                        clipBoardContent += "";
                        clipBoardContent += window.location.href;
                        window.clipboardData.setData("Text", clipBoardContent);
                        alert('复制成功,请黏贴推荐给你的好友领取优惠券!');
                    }
                    else {
                        prompt("请复制弹出框里面网站地址:", window.location.href);
                    }

                }

 //方法二
                function copy_clip() {
                    var url = window.location.href;     //需要复制的内容
                    var txt = url;
                    if (window.clipboardData) {
                        window.clipboardData.clearData();
                        window.clipboardData.setData("Text", txt);
                        alert('恭喜,复制成功!');
                    } else if (navigator.userAgent.indexOf("Opera") != -1) {
                        window.location = txt;
                    } else if (window.netscape) {
                        try {
                            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                        } catch (e) {
                            alert("您的firefox安全限制限制您进行剪贴板操作,请在新窗口的地址栏里输入'about:config'然后找到'signed.applets.codebase_principal_support'设置为true'");
                            return false;
                        }
                        var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
                        if (!clip)
                            return;
                        var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
                        if (!trans)
                            return;
                        trans.addDataFlavor('text/unicode');
                        var str = new Object();
                        var len = new Object();
                        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                        var copytext = txt;
                        str.data = copytext;
                        trans.setTransferData("text/unicode", str, copytext.length * 2);
                        var clipid = Components.interfaces.nsIClipboard;
                        if (!clip)
                            return false;
                        clip.setData(trans, null, clipid.kGlobalClipboard);

                    }
                }

5、倒数60秒

        //倒数六十秒
        var i = 61;
        function send() {
            i--;
            if (i == -1) {
                return null;
            }
            else {
                document.getElementById("btnCode").value = i;
                setTimeout("send();", 1000);
            }

        }