快捷搜索:

28个常用JavaScript方法集锦_javascript技巧_脚本之家

2019-12-04 作者:白姐四不像必中一肖图   |   浏览(71)

平常使用的 JS 方法,今天记录,以便今后查询

/* 手提式无线电话机类型推断 */

复制代码 代码如下:var BrowserInfo = { userAgent: navigator.userAgent.toLowerCase(卡塔尔(قطر‎ isAndroid: Boolean(navigator.userAgent.match, isIphone: Boolean(navigator.userAgent.match, isIpad: Boolean(navigator.userAgent.match, isWeixin: Boolean(navigator.userAgent.match,}

/* 再次回到字符串长度,男子计数为2 */

复制代码 代码如下:function strLength { var a = 0; for (var i = 0; i < str.length; i++卡塔尔(قطر‎ { if > 255卡塔尔(قطر‎ a += 2;//依照预期计数扩充2 else a++; } return a;}

获得url中的参数

复制代码 代码如下:function GetQueryStringRegExp { var reg = new RegExp" + name + "=", "i"卡塔尔(قطر‎; if return decodeUHavalIComponent(RegExp.$2.replace; return "";}

/* js 绑定事件 适用于其余浏览器的因素绑定 */

复制代码 代码如下:function eventBind(obj, eventType, callBack卡塔尔(英语:State of Qatar) { if { obj.addEventListener(eventType, callBack, false卡塔尔; } else if { obj.attach伊夫nt('on' + eventType, callBack卡塔尔国; } else { obj['on' + eventType] = callBack; } };eventBind(document, 'click', bodyClick);

/* 取安妥前浏览器JS的版本 */

复制代码 代码如下:function getjsversion(卡塔尔(قطر‎{ var n = navigator; var u = n.userAgent; var apn = n.appName; var v = n.appVersion; var ie = v.indexOf{ apv = parseInt(i = v.substring; if { apv = parseFloat; } } else { apv = parseFloat; } var isie = (apn == 'Microsoft Internet Explorer'卡塔尔国; var ismac = >= 0卡塔尔(英语:State of Qatar); var javascriptVersion = "1.0"; if (String && String.prototype卡塔尔(英语:State of Qatar) { javascriptVersion = '1.1'; if (javascriptVersion.match卡塔尔 { javascriptVersion = '1.2'; var tm = new Date; if { javascriptVersion = '1.3'; if (isie && ismac && apv >= 5卡塔尔(英语:State of Qatar) javascriptVersion = '1.4'; var pn = 0; if { javascriptVersion = '1.5'; a = new Array; if { javascriptVersion = '1.6'; i = 0; o = new Object; tcf = new Function('o', 'var e,i=0;try{i=new Iterator{}return i'卡塔尔国; i = tcf { javascriptVersion = '1.7'; } } } } } } return javascriptVersion;}

/* 获取当前点击事件的Object对象 */

复制代码 代码如下:function getEvent { return window.event; //假如是ie } func = getEvent.caller; while { var arg0 = func.arguments[0]; if { if ((arg0.constructor == Event || arg0.constructor == MouseEvent)|| == "object" && arg0.preventDefault && arg0.stopPropagation)) { return arg0; } } func = func.caller; } return null;};

/* 字符串截取方法 */

复制代码 代码如下:getCharactersLen: function { if (charStr == null || charStr == ''卡塔尔(英语:State of Qatar) return ''; var totalCount = 0; var newStr = ''; for (var i = 0; i < charStr.length; i++卡塔尔(قطر‎ { var c = charStr.charCodeAt; if { totalCount++; } else { totalCount += 2; } if (totalCount >= cutCount卡塔尔(英语:State of Qatar) { newStr += charStr.charAt; break; } else { newStr += charStr.charAt; } } return newStr; }

/* JS 弹出新窗口全屏 */

复制代码 代码如下:var tmp = window.open("about:blank", "", "fullscreen=1"卡塔尔(قطر‎ tmp.moveTo; tmp.resizeTo(screen.width + 20, screen.height卡塔尔国; tmp.focus(卡塔尔国; tmp.location.href = '' + msgResult.message[0] + '.html';var config_ = "left=0,top=0,width=" +

  • ",height=" + ; window.open('' + msgResult.message[0] + '.html', "winHanle", config_卡塔尔(قطر‎;//模拟form提交展开新页面var f = document.createElement; f.setAttribute('action', '' + msgResult.message[0] + '.html'); f.target = '_blank'; document.body.appendChild;

/* 全选/全不选 */

复制代码 代码如下:function selectAll { if (objSelect.checked == true卡塔尔(قطر‎ { $("input[name='chkId']").attr; $("input[name='chkAll']").attr; } else if (objSelect.checked == false) { $("input[name='chkId']").attr; $("input[name='chkAll']").attr; } }

/* js 剖断浏览器 */

复制代码 代码如下:判别是不是是 IE 浏览器 if { alert; }else{ alert; } if (!!window.ActiveXObject){ alert; }else{ alert; }判断是IE几var isIE=!!window.ActiveXObject;var isIE6=isIE&&!window.XMLHttpRequest;var isIE8=isIE&&!!document.documentMode;var isIE7=isIE&&!isIE6&&!isIE8;if { alert; }else if ; }else if ; }}

/* 剖断浏览器 */

复制代码 代码如下:function getOs(卡塔尔(英语:State of Qatar) { if (navigator.userAgent.indexOf { return "MSIE8"; } else if (navigator.userAgent.indexOf { return "MSIE6"; } else if (navigator.userAgent.indexOf { return "MSIE7"; } else if (isFirefox = navigator.userAgent.indexOf { return "Firefox"; } if (navigator.userAgent.indexOf { return "Chrome"; } else { return "Other"; }}

/* JS判定七个日子大小 切合 2012-09-09 与二〇一三-9-9 三种格式的对照 */

复制代码 代码如下://拿到日期值并转产生日期格式,replace是依附表达表明式把日子转变成长日期格式,那样再拓宽推断就好剖断了 function ValidateDate(卡塔尔国 { var beginDate = $; var endDate = $; if (beginDate.length > 0 && endDate.length>0卡塔尔(英语:State of Qatar) { var sDate = new Date(beginDate.replace; var eDate= new Date(endDate.replace; if { alert; return false; } } }

/* 移除事件 */

复制代码 代码如下:this.moveBind = function (objId, eventType, callBack卡塔尔(英语:State of Qatar) { var obj = document.getElementById; if (obj.removeEventListener卡塔尔(英语:State of Qatar) { obj.removeEventListener(eventType, callBack, false卡塔尔(قطر‎; } else if { obj.detach伊芙nt('on' + eventType, callBack卡塔尔(قطر‎; } else { obj['on' + eventType] = null; }}

/* 回车提交 */

复制代码 代码如下:$.onkeypress = function { event = ? event : ? window.event : ""卡塔尔(قطر‎ keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode卡塔尔(قطر‎; if { $.onclick(卡塔尔; }}

/* JS 实施反应计时器 */

复制代码 代码如下:timeStart = new Date;timesEnd = new Date;document.getElementById.innerHTML = timesEnd - timeStart;

/* JS 写Cookie */

复制代码 代码如下:function setCookie(name, value, expires, path, domain卡塔尔(قطر‎ { if expires = -1; if path = "/"; var d = "" + name + "=" + value; var e; if { e = ""; } else if { var f = new Date; e = ";expires=" + f.toUTCString(卡塔尔; } else { var now = new Date(卡塔尔; var f = new Date + expires * 1000); e = ";expires=" + f.toUTCString(); } var dm; if { dm = ""; } else { dm = ";domain=" + domain; } document.cookie = name + "=" + value + ";path=" + path + e + dm;};

/* JS 读Cookie */

复制代码 代码如下:function readCookie { var nameEQ = name + "="; var ca = document.cookie.split; for (var i = 0; i < ca.length; i++卡塔尔(قطر‎ { var c = ca[i]; while c = c.substring; if == 0) { return decodeURIComponent(c.substring(nameEQ.length, c.length)) } } return null}

/* Ajax 请求 */

复制代码 代码如下:C.ajax = function { 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 { try { xmlhttp = new ActiveXObject; } catch { xmlHttp = new ActiveXObject; } } if { alert; return false; } return xmlhttp; }; this.send = function (卡塔尔国 { C.each(self.options, function { self.options[key] = ? val : args[key]; }); var xmlHttp = new self.getXmlHttp(); if (self.options.type.toUpperCase { xmlHttp.open(self.options.type, self.options.url + (self.options.data == null ? "" : ((/[?]$/.test ? '&' : '?') + 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 = 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; }; this.send();};

/* JS StringBuilder 用法 */

复制代码 代码如下:function StringBuilder(卡塔尔(قطر‎ { this.strings = new Array;};StringBuilder.prototype.append = function { this.strings.push;};StringBuilder.prototype.toString = function (卡塔尔(قطر‎ { return this.strings.join;};

/* JS 加载到最上端LoadJS */

复制代码 代码如下:function loadJS { var ss = document.getElementsByName, loaded = false; for (var i = 0, len = ss.length; i < len; i++卡塔尔(قطر‎ { if (ss[i].src && ss[i].getAttribute { loaded = true; break; } } if { if (fn && typeof fn != 'undefined' && fn instanceof Function) fn(); return false; } var s = document.createElement, b = false; s.setAttribute('type', 'text/javascript'); s.setAttribute; 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[0].appendChild; }, bind: function (objId, eventType, callBack卡塔尔(قطر‎ { //适用于其他浏览器的绑定 var obj = document.getElementById; if { obj.addEventListener(eventType, callBack, false卡塔尔(英语:State of Qatar); } else if { obj.attachEvent('on' + eventType, callBack卡塔尔国; } else { obj['on' + eventType] = callBack; } }function JSLoad { s = document.createElement; s.setAttribute("type", "text/javascript"); s.setAttribute; s.onload = s.onreadystatechange = function () { if (!s.readyState || s.readyState == "loaded" || s.readyState == "complete") { if (typeof args.callback == "function") args.callback; s.onload = s.onreadystatechange = null; try { s.parentNode && s.parentNode.removeChild { } } }; document.getElementsByTagName[0].appendChild; }

/* 清空 LoadJS 加载到顶端的js援用 */

复制代码 代码如下:function ClearHeadJs { var js = document.getElementsByTagName[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 { obj = js[i]; } } document.getElementsByTagName[0].removeChild; };

/* JS 替换违规字符首要用在密码验证上面世的特殊字符 */

复制代码 代码如下:function UEnclaveLencode { return escape.replace.replace.replace.replace;};

/* 按Ctrl + Entert 间接交给表单 */

复制代码 代码如下:document.body.onkeydown = function { evt = evt ? evt : (window.event ? window.event : null卡塔尔国; if (13 == evt.keyCode && evt.ctrlKey卡塔尔 { evt.returnValue = false; evt.cancel = true; PostData(卡塔尔国; }}; /* 获取当前岁月 */

复制代码 代码如下:function GetCurrentDate(卡塔尔国 { var d = new Date(卡塔尔; var y = d.getYear(卡塔尔+一九〇二; month = add_zero, days = add_zero, hours = add_zero; minutes = add_zero, seconds = add_zero; var str = y + '-' + month + '-' + days + ' ' + hours + ':' + minutes + ':' + seconds; return str; }; function add_zero { if return "0" + temp; else return temp; } /* Js 去掉空格方法: */

复制代码 代码如下:String.prototype.Trim = function(卡塔尔(英语:State of Qatar){ return this.replace; }String.prototype.LTrim = function(卡塔尔国{return this.replace;}String.prototype.RTrim = function(卡塔尔{return this.replace;}

/* js 动态移除 head 里的 js 援用 */

复制代码 代码如下:this.ClearHeadJs = function { var js = document.getElementsByTagName[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 { obj = js[i]; } } document.getElementsByTagName[0].removeChild;};

/* 整个UL 点击事件 加在UL里的onclick里 */

复制代码 代码如下:function CreateFrom { var f = document.createElement; f.setAttribute; for (var i = 0; i < params.length; i++卡塔尔 { var input = document.createElement; input.setAttribute; input.setAttribute("name", params[i].paramName); input.setAttribute("value", params[i].paramValue); f.appendChild; } f.target = "_blank"; document.body.appendChild;};

/* 判别浏览器选拔的是哪些 JS 版本 */

复制代码 代码如下:

alert;

如上正是本文的全体内容了,希望能对大家有着支持,也不枉收拾这么久了,呵呵。

本文由正版香港马报免费资料发布于白姐四不像必中一肖图,转载请注明出处:28个常用JavaScript方法集锦_javascript技巧_脚本之家

关键词: