摘要:轉(zhuǎn)自攻城獅常用方法總結(jié)手機(jī)類型判斷返回字符串長度,漢字計(jì)數(shù)為按照預(yù)期計(jì)數(shù)增加獲取中的參數(shù)綁定事件適用于任何瀏覽器的元素綁定獲得當(dāng)前瀏覽器的版本獲取當(dāng)前點(diǎn)擊事件的對象如果是字符串截取方法彈出新窗口全屏模擬提交打開新頁面全選全不選判斷瀏覽器判
轉(zhuǎn)自 攻城獅-JavaScript 常用方法總結(jié)
手機(jī)類型判斷var BrowserInfo = { userAgent: navigator.userAgent.toLowerCase() isAndroid: Boolean(navigator.userAgent.match(/android/ig)), isIphone: Boolean(navigator.userAgent.match(/iphone|ipod/ig)), isIpad: Boolean(navigator.userAgent.match(/ipad/ig)), isWeixin: Boolean(navigator.userAgent.match(/MicroMessenger/ig)), }返回字符串長度,漢字計(jì)數(shù)為2
js 綁定事件 適用于任何瀏覽器的元素綁定
function strLength(str) { var a = 0; for (var i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 255) a += 2;//按照預(yù)期計(jì)數(shù)增加2 else a++; } return a; } 獲取url中的參數(shù) function GetQueryStringRegExp(name,url) { var reg = new RegExp("(^|?|&)" + name + "=([^&]*)(s|&|$)", "i"); if (reg.test(url)) return decodeURIComponent(RegExp.$2.replace(/+/g, " ")); return ""; }
獲得當(dāng)前瀏覽器JS的版本
function eventBind(obj, eventType, callBack) { if (obj.addEventListener) { obj.addEventListener(eventType, callBack, false); } else if (window.attachEvent) { obj.attachEvent("on" + eventType, callBack); } else { obj["on" + eventType] = callBack; } }; eventBind(document, "click", bodyClick);
function getjsversion(){ var n = navigator; var u = n.userAgent; var apn = n.appName; var v = n.appVersion; var ie = v.indexOf("MSIE "); if (ie > 0){ apv = parseInt(i = v.substring(ie + 5)); if (apv > 3) { apv = parseFloat(i); } } else { apv = parseFloat(v); } var isie = (apn == "Microsoft Internet Explorer"); var ismac = (u.indexOf("Mac") >= 0); var javascriptVersion = "1.0"; if (String && String.prototype) { javascriptVersion = "1.1"; if (javascriptVersion.match) { javascriptVersion = "1.2"; var tm = new Date; if (tm.setUTCDate) { javascriptVersion = "1.3"; if (isie && ismac && apv >= 5) javascriptVersion = "1.4"; var pn = 0; if (pn.toPrecision) { javascriptVersion = "1.5"; a = new Array; if (a.forEach) { javascriptVersion = "1.6"; i = 0; o = new Object; tcf = new Function("o", "var e,i=0;try{i=new Iterator(o)}catch(e){}return i"); i = tcf(o); if (i && i.next) { javascriptVersion = "1.7"; } } } } } } return javascriptVersion; }獲取當(dāng)前點(diǎn)擊事件的Object對象
function getEvent() { if (document.all) { return window.event; //如果是ie } func = getEvent.caller; while (func != null) { var arg0 = func.arguments[0]; if (arg0) { if ((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) { return arg0; } } func = func.caller; } return null; };字符串截取方法
getCharactersLen: function (charStr, cutCount) { if (charStr == null || charStr == "") return ""; var totalCount = 0; var newStr = ""; for (var i = 0; i < charStr.length; i++) { var c = charStr.charCodeAt(i); if (c < 255 && c > 0) { totalCount++; } else { totalCount += 2; } if (totalCount >= cutCount) { newStr += charStr.charAt(i); break; } else { newStr += charStr.charAt(i); } } return newStr; }JS 彈出新窗口全屏
全選/全不選
var tmp = window.open("about:blank", "", "fullscreen=1") tmp.moveTo(0, 0); tmp.resizeTo(screen.width + 20, screen.height); tmp.focus(); tmp.location. + msgResult.message[0] + ".html"; var config_ = "left=0,top=0,width=" + (window.screen.Width) + ",height=" + (window.screen.Height); window.open("http://www.che168.com/pinggu/eva_" + msgResult.message[0] + ".html", "winHanle", config_); //模擬form提交打開新頁面 var f = document.createElement("form"); f.setAttribute("action", "http://www.che168.com/pinggu/eva_" + msgResult.message[0] + ".html"); f.target = "_blank"; document.body.appendChild(f); f.submit();
js 判斷瀏覽器
function selectAll(objSelect) { if (objSelect.checked == true) { $("input[name="chkId"]").attr("checked", true); $("input[name="chkAll"]").attr("checked", true); } else if (objSelect.checked == false) { $("input[name="chkId"]").attr("checked", false); $("input[name="chkAll"]").attr("checked", false); } }
判斷是否是 IE 瀏覽器 if (document.all){ alert(”IE瀏覽器”); }else{ alert(”非IE瀏覽器”); } if (!!window.ActiveXObject){ alert(”IE瀏覽器”); }else{ alert(”非IE瀏覽器”); } 判斷是IE幾 var isIE=!!window.ActiveXObject; var isIE6=isIE&&!window.XMLHttpRequest; var isIE8=isIE&&!!document.documentMode; var isIE7=isIE&&!isIE6&&!isIE8; if (isIE){ if (isIE6){ alert(”ie6″); }else if (isIE8){ alert(”ie8″); }else if (isIE7){ alert(”ie7″); } }判斷瀏覽器
JS判斷兩個(gè)日期大小 適合 2012-09-09 與2012-9-9 兩種格式的對比
function getOs() { if (navigator.userAgent.indexOf("MSIE 8.0") > 0) { return "MSIE8"; } else if (navigator.userAgent.indexOf("MSIE 6.0") > 0) { return "MSIE6"; } else if (navigator.userAgent.indexOf("MSIE 7.0") > 0) { return "MSIE7"; } else if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) { return "Firefox"; } if (navigator.userAgent.indexOf("Chrome") > 0) { return "Chrome"; } else { return "Other"; } }
移除事件
11 12 13 //得到日期值并轉(zhuǎn)化成日期格式,replace(/-/g, "/")是根據(jù)驗(yàn)證表達(dá)式把日期轉(zhuǎn)化成長日期格式,這樣再進(jìn)行判斷就好判斷了 function ValidateDate() { var beginDate = $("#t_datestart").val(); var endDate = $("#t_dateend").val(); if (beginDate.length > 0 && endDate.length>0) { var sDate = new Date(beginDate.replace(/-/g, "/")); var eDate= new Date(endDate.replace(/-/g, "/")); if (sDate > eDate) { alert("開始日期要小于結(jié)束日期"); return false; } } }
回車提交
this.moveBind = function (objId, eventType, callBack) { var obj = document.getElementById(objId); if (obj.removeEventListener) { obj.removeEventListener(eventType, callBack, false); } else if (window.detachEvent) { obj.detachEvent("on" + eventType, callBack); } else { obj["on" + eventType] = null; } }
JS 執(zhí)行計(jì)時(shí)器
$("id").onkeypress = function (event) { event = (event) ? event : ((window.event) ? window.event : "") keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode); if (keyCode == 13) { $("SubmitLogin").onclick(); } }
timeStart = new Date().getTime(); timesEnd = new Date().getTime(); document.getElementById("time").innerHTML = timesEnd - timeStart;JS 寫Cookie
JS 讀Cookie
function setCookie(name, value, expires, path, domain) { if (!expires) expires = -1; if (!path) path = "/"; var d = "" + name + "=" + value; var e; if (expires < 0) { e = ""; } else if (expires == 0) { var f = new Date(1970, 1, 1); e = ";expires=" + f.toUTCString(); } else { var now = new Date(); var f = new Date(now.getTime() + expires * 1000); e = ";expires=" + f.toUTCString(); } var dm; if (!domain) { dm = ""; } else { dm = ";domain=" + domain; } document.cookie = name + "=" + value + ";path=" + path + e + dm; };
Ajax 請求
function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(";"); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == " ") c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) { return decodeURIComponent(c.substring(nameEQ.length, c.length)) } } return null }
JS StringBuilder 用法
C.ajax = function (args) { var self = this; this.options = { type: "GET", async: true, contentType: "application/x-www-form-urlencoded", url: "about:blank", data: null, success: {}, error: {} }; this.getXmlHttp = function () { var xmlHttp; try { xmlhttp = new XMLHttpRequest(); } catch (e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } if (!xmlhttp) { alert("您的瀏覽器不支持AJAX"); return false; } return xmlhttp; }; this.send = function () { C.each(self.options, function (key, val) { self.options[key] = (args[key] == null) ? val : args[key]; }); var xmlHttp = new self.getXmlHttp(); if (self.options.type.toUpperCase() == "GET") { xmlHttp.open(self.options.type, self.options.url + (self.options.data == null ? "" : ((/[?]$/.test(self.options.url) ? "&" : "?") + self.options.data)), self.options.async); } else { xmlHttp.open(self.options.type, self.options.url, self.options.async); xmlHttp.setRequestHeader("Content-Length", self.options.data.length); } xmlHttp.setRequestHeader("Content-Type", self.options.contentType); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200 || xmlHttp.status == 0) { if (typeof self.options.success == "function") self.options.success(xmlHttp.responseText); xmlHttp = null; } else { if (typeof self.options.error == "function") self.options.error("Server Status: " + xmlHttp.status); } } }; xmlHttp.send(self.options.type.toUpperCase() == "POST" ? self.options.data.toString() : null); }; this.send(); };
function StringBuilder() { this.strings = new Array; }; StringBuilder.prototype.append = function (str) { this.strings.push(str); }; StringBuilder.prototype.toString = function () { return this.strings.join(""); };JS 加載到頂部LoadJS
清空 LoadJS 加載到頂部的js引用
function loadJS (url, fn) { var ss = document.getElementsByName("script"), loaded = false; for (var i = 0, len = ss.length; i < len; i++) { if (ss[i].src && ss[i].getAttribute("src") == url) { loaded = true; break; } } if (loaded) { if (fn && typeof fn != "undefined" && fn instanceof Function) fn(); return false; } var s = document.createElement("script"), b = false; s.setAttribute("type", "text/javascript"); s.setAttribute("src", url); s.onload = s.onreadystatechange = function () { if (!b && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) { b = true; if (fn && typeof fn != "undefined" && fn instanceof Function) fn(); } }; document.getElementsByTagName("head")[0].appendChild(s); }, bind: function (objId, eventType, callBack) { //適用于任何瀏覽器的綁定 var obj = document.getElementById(objId); if (obj.addEventListener) { obj.addEventListener(eventType, callBack, false); } else if (window.attachEvent) { obj.attachEvent("on" + eventType, callBack); } else { obj["on" + eventType] = callBack; } } function JSLoad (args) { s = document.createElement("script"); s.setAttribute("type", "text/javascript"); s.setAttribute("src", args.url); s.onload = s.onreadystatechange = function () { if (!s.readyState || s.readyState == "loaded" || s.readyState == "complete") { if (typeof args.callback == "function") args.callback(this, args); s.onload = s.onreadystatechange = null; try { s.parentNode && s.parentNode.removeChild(s); } catch (e) { } } }; document.getElementsByTagName("head")[0].appendChild(s); }
JS 替換非法字符主要用在密碼驗(yàn)證上出現(xiàn)的特殊字符
function ClearHeadJs (src) { var js = document.getElementsByTagName("head")[0].children; var obj = null; for (var i = 0; i < js.length; i++) { if (js[i].tagName.toLowerCase() == "script" && js[i].attributes["src"].value.indexOf(src) > 0) { obj = js[i]; } } document.getElementsByTagName("head")[0].removeChild(obj); };
function URLencode(sStr) { return escape(sStr).replace(/+/g, "%2B").replace(/"/g, "%22").replace(/"/g, "%27").replace(///g, "%2F"); };按Ctrl + Entert 直接提交表單
獲取當(dāng)前時(shí)間
document.body.onkeydown = function (evt) { evt = evt ? evt : (window.event ? window.event : null); if (13 == evt.keyCode && evt.ctrlKey) { evt.returnValue = false; evt.cancel = true; PostData(); } };
Js 去掉空格方法:
function GetCurrentDate() { var d = new Date(); var y = d.getYear()+1900; month = add_zero(d.getMonth() + 1), days = add_zero(d.getDate()), hours = add_zero(d.getHours()); minutes = add_zero(d.getMinutes()), seconds = add_zero(d.getSeconds()); var str = y + "-" + month + "-" + days + " " + hours + ":" + minutes + ":" + seconds; return str; }; function add_zero(temp) { if (temp < 10) return "0" + temp; else return temp; }
js 動(dòng)態(tài)移除 head 里的 js 引用
String.prototype.Trim = function() { return this.replace(/(^s*)|(s*$)/g, ""); } String.prototype.LTrim = function() {return this.replace(/(^s*)/g, "");} String.prototype.RTrim = function() {return this.replace(/(s*$)/g, "");}
整個(gè)UL 點(diǎn)擊事件 加在UL里的onclick里
this.ClearHeadJs = function (src) { var js = document.getElementsByTagName("head")[0].children; var obj = null; for (var i = 0; i < js.length; i++) { if (js[i].tagName.toLowerCase() == "script" && js[i].attributes["src"].value.indexOf(src) > 0) { obj = js[i]; } } document.getElementsByTagName("head")[0].removeChild(obj); };
判斷瀏覽器使用的是哪個(gè) JS 版本
function CreateFrom(url, params) { var f = document.createElement("form"); f.setAttribute("action", url); for (var i = 0; i < params.length; i++) { var input = document.createElement("input"); input.setAttribute("type", "hidden"); input.setAttribute("name", params[i].paramName); input.setAttribute("value", params[i].paramValue); f.appendChild(input); } f.target = "_blank"; document.body.appendChild(f); f.submit(); };
alert(jsversion);
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/91454.html
摘要:插件性能優(yōu)化及個(gè)人常用優(yōu)化方法經(jīng)常會(huì)觸發(fā)視覺變化。作用域鏈指的是當(dāng)前作用于下可用變量的集合,它在各種主流瀏覽器中至少包含兩個(gè)部分局部變量的集合和全局變量的集合。在考慮優(yōu)化時(shí),數(shù)值和變量的性能差不多,并且速度顯著優(yōu)于對象屬性和數(shù)組元素。 JavaScript 插件性能優(yōu)化及個(gè)人react常用優(yōu)化方法 JavaScript 經(jīng)常會(huì)觸發(fā)視覺變化。有時(shí)是直接通過樣式操作,有時(shí)是會(huì)產(chǎn)生視覺變化...
摘要:一可以用作對象的復(fù)制可以用作對象的合并注意目標(biāo)對象自身也會(huì)改變。對象四返回一個(gè)數(shù)組,包括對象自身的不含繼承的所有可枚舉屬性不含屬性的鍵名。該方法返回被凍結(jié)的對象。方法判斷一個(gè)對象是否被凍結(jié)。 JavaScript對Object對象的一些常用操作總結(jié)。 一、Object.assign() 1.可以用作對象的復(fù)制 var obj = { a: 1 }; var copy = Object....
摘要:返回值一個(gè)對象集合,包含六個(gè)屬性上下左右寬高注意只用于行內(nèi)元素只能獲取樣式只讀獲取元素位置獲得頁面中某個(gè)元素的左,上,右和下分別相對瀏覽器視窗的位置。返回一個(gè)類數(shù)組對象,其包含樣式表中所有規(guī)則。 [TOC] 獲取樣式 元素對象的寬高位置距離等屬性 如offsetWidht、cilentWidht、scrollWidth…… let oWidth=obj.offsetWidth; 注意...
摘要:系列種優(yōu)化頁面加載速度的方法隨筆分類中個(gè)最重要的技術(shù)點(diǎn)常用整理網(wǎng)頁性能管理詳解離線緩存簡介系列編寫高性能有趣的原生數(shù)組函數(shù)數(shù)據(jù)訪問性能優(yōu)化方案實(shí)現(xiàn)的大排序算法一怪對象常用方法函數(shù)收集數(shù)組的操作面向?qū)ο蠛驮屠^承中關(guān)鍵詞的優(yōu)雅解釋淺談系列 H5系列 10種優(yōu)化頁面加載速度的方法 隨筆分類 - HTML5 HTML5中40個(gè)最重要的技術(shù)點(diǎn) 常用meta整理 網(wǎng)頁性能管理詳解 HTML5 ...
摘要:系列種優(yōu)化頁面加載速度的方法隨筆分類中個(gè)最重要的技術(shù)點(diǎn)常用整理網(wǎng)頁性能管理詳解離線緩存簡介系列編寫高性能有趣的原生數(shù)組函數(shù)數(shù)據(jù)訪問性能優(yōu)化方案實(shí)現(xiàn)的大排序算法一怪對象常用方法函數(shù)收集數(shù)組的操作面向?qū)ο蠛驮屠^承中關(guān)鍵詞的優(yōu)雅解釋淺談系列 H5系列 10種優(yōu)化頁面加載速度的方法 隨筆分類 - HTML5 HTML5中40個(gè)最重要的技術(shù)點(diǎn) 常用meta整理 網(wǎng)頁性能管理詳解 HTML5 ...
閱讀 3729·2021-09-22 15:49
閱讀 3300·2021-09-08 09:35
閱讀 1422·2019-08-30 15:55
閱讀 2321·2019-08-30 15:44
閱讀 714·2019-08-29 16:59
閱讀 1597·2019-08-29 16:16
閱讀 479·2019-08-28 18:06
閱讀 890·2019-08-27 10:55