Javascript常用方法函数集合(二)
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
"h+":this.getHours(),//hour
"m+":this.getMinutes(),//minute
"s+":this.getSeconds(),//second
"q+":Math.floor((this.getMonth()+3)/3),//quarter
"S":this.getMilliseconds()//millisecond
vard=document,a=patMode=="BackCompat"
?d.body
:d.documentElement;
returna.clientWidth;
}
41.获取页面宽度
functiongetPageWidth(){
varg=document,a=g.body,f=g.documentElement,d=patMode=="BackCompat"
iCode=sStr.charCodeAt(i);
if(iCode==12288){
iCode=32;
}elseif(iCode>65280&&iCode<65375){
iCode-=65248;
}
oRs.push(String.fromCharCode(iCode));
}
}
returnoRs.join("");
returnscreenWidth/window.innerWidth;
}else{
returnscreenWidth/document.body.offsetWidth;
}
}
35.获取移动设备屏幕宽度
functiongetScreenWidth(){
varsmallerSide=Math.min(screen.width,screen.height);
}
43.获取页面可视高度
functiongetPageViewHeight(){
vard=document,a=patMode=="BackCompat"
?d.body
:d.documentElement;
returna.clientHeight;
}
44.去掉url前缀
functionremoveUrlPrefix(a){
?a
:g.documentElement;
returnMath.max(f.scrollWidth,a.scrollWidth,d.clientWidth);
}
42.获取页面lTop
functiongetPageScrollTop(){
vara=document;
returna.documentElement.scrollTop||a.body.scrollTop;
varfixViewPortsExperiment=rendererModel.runningExperiments.FixViewport||rendererModel.runningExperiments.fixviewport;
varfixViewPortsExperimentRunning=fixViewPortsExperiment&&(fixViewPortsExperiment.toLowerCase()==="new");
}
39.获取页面scrollLeft
functiongetPageScrollLeft(){
vara=document;
returna.documentElement.scrollLeft||a.body.scrollLeft;
}
40.获取页面可视宽度
functiongetPageViewWidth(){
break
}
}
}
}
returnels
}
38.获取页面高度
functiongetPageHeight(){
varg=document,a=g.body,f=g.documentElement,d=patMode=="BackCompat"
?a
:g.documentElement;
returnMath.max(f.scrollHeight,a.scrollHeight,d.clientHeight);
returnfalse;
}
48.获取网页被卷去的位置
functiongetScrollXY(){
returndocument.body.scrollTop?{
x:document.body.scrollLeft,
y:document.body.scrollTop
}:{
x:document.documentElement.scrollLeft,
if(regular.test(strUrl)){
returntrue;
}else{
returnfalse;
}
}
37.getElementsByClassName
functiongetElementsByClassName(name){
vartags=document.getElementsByTagName('*')||document.all;
if(this.isAndroidMobileDevice()&&!this.isNewChromeOnAndroid()){
screenWidth=screenWidth/window.devicePixelRatio;
}
varFixViewPortsExperiment=rendererModel.runningExperiments.FixViewport||rendererModel.runningExperiments.fixviewport;
}
returnthis._initZoom;
}
34.获取移动设备最大化大小
functiongetZoom(){
varscreenWidth=(Math.abs(window.orientation)===90)?Math.max(screen.height,screen.width):Math.min(screen.height,screen.width);
functionchgCase(sStr,iCase){
if(typeofsStr!="string"||sStr.length<=0||!(iCase===0||iCase==1)){
returnsStr;
}
vari,oRs=[],iCode;
if(iCase){/*半->全*/
for(i=0;i<sStr.length;i+=1){
}
32.判断是否打开视窗
functionisViewportOpen(){
return!!document.getElementById('wixMobileViewport');
}
33.获取移动设备初始化大小
functiongetInitZoom(){
if(!this._initZoom){
varscreenWidth=Math.min(screen.height,screen.width);
varFixViewPortsExperimentRunning=FixViewPortsExperiment&&(FixViewPortsExperiment==="New"||FixViewPortsExperiment==="new");
if(FixViewPortsExperimentRunning){
y:document.documentElement.scrollTop
}
}
49.日期格式化函数+调用方法
Date.prototype.format=function(format){
varo={
"M+":this.getMonth()+1,//month
"d+":this.getDate(),//day
Javascript
在上一篇文章《Javascript常用方法函数收集(一)》中,作者分享了30个Javascript常用方法函数。在本文中,我们将继续上篇中的JS常用方法函数进行分享,希望能对学习前端的朋友们有所帮助。
31.判断是否Touch屏幕
functionisTouchScreen(){
return(('ontouchstart'inwindow)||window.DocumentTouch&&documentinstanceofDocumentTouch);
iCode=sStr.charCodeAt(i);
if(iCode==32){
iCode=12288;
}elseif(iCode<127){
iCode+=65248;
}
oRs.push(String.fromCharCode(iCode));
}
}else{/*全->半*/
for(i=0;i<sStr.length;i+=1){
if(iKey>95&&iKey<108){returntrue;}/*数字键盘1*/
if(iKey>108&&iKey<112){returntrue;}/*数字键盘2*/
if(iKey>185&&iKey<193){returntrue;}/*符号1*/
if(iKey>218&&iKey<223){returntrue;}/*符号2*/
}
47.确认是否键盘有效输入值
functioncheckKey(iKey){
if(iKey==32||iKey==229){returntrue;}/*空格和异常*/
if(iKey>47&&iKey<58){returntrue;}/*数字*/
if(iKey>64&&iKey<91){returntrue;}/*字母*/
if(fixViewPortsExperiment){
if(this.isAndroidMobileDevice()&&!this.isNewChromeOnAndroid()){
smallerSide=smallerSide/window.devicePixelRatio;
}
}
returnsmallerSide;
}
45.随机数时间戳
functionuniqueId(){
vara=Math.random,b=parseInt;
returnNumber(newDate()).toString()+b(10*a())+b(10*a())+b(10*a());
}
46.全角半角转换
//iCase: 0全到半,1半到全,其他不转化
varels=[];
for(vari=0;i<tags.length;i++){
if(tags.className){
varcs=tags.className.split(' ');
for(varj=0;j<cs.length;j++){
if(name==cs[j]){
els.push(tags);
if(this.isAndroidMobileDevice()&&!this.isNewChromeOnAndroid()){
screenWidth=screenWidth/window.devicePixelRatio;
}
this._initZoom=screenWidth/document.body.offsetWidth;
a=a.replace(/:/g,":").replace(/./g,".").replace(///g,"/");
while(trim(a).toLowerCase().indexOf("http://")==0){
a=trim(a.replace(/http:\/\//i,""));
}
returna;
}
36.完美判断是否为网址
functionIsURL(strUrl){
varregular=/^\b(((https?|ftp):\/\/)?[-a-z0-9]+(\.[-a-z0-9]+)*\.(?:com|edu|gov|int|mil|net|org|biz|info|name|museum|asia|coop|aero|[a-z][a-z]|((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d))\b(\/[-a-z0-9_:\@&?=+,.!\/~%\$]*)?)$/i
};
if(/(y+)/.test(format))format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4-RegExp.$1.length));
for(varkino){
if(newRegExp("("+k+")").test(format))
"m+":this.getMinutes(),//minute
"s+":this.getSeconds(),//second
"q+":Math.floor((this.getMonth()+3)/3),//quarter
"S":this.getMilliseconds()//millisecond
vard=document,a=patMode=="BackCompat"
?d.body
:d.documentElement;
returna.clientWidth;
}
41.获取页面宽度
functiongetPageWidth(){
varg=document,a=g.body,f=g.documentElement,d=patMode=="BackCompat"
iCode=sStr.charCodeAt(i);
if(iCode==12288){
iCode=32;
}elseif(iCode>65280&&iCode<65375){
iCode-=65248;
}
oRs.push(String.fromCharCode(iCode));
}
}
returnoRs.join("");
returnscreenWidth/window.innerWidth;
}else{
returnscreenWidth/document.body.offsetWidth;
}
}
35.获取移动设备屏幕宽度
functiongetScreenWidth(){
varsmallerSide=Math.min(screen.width,screen.height);
}
43.获取页面可视高度
functiongetPageViewHeight(){
vard=document,a=patMode=="BackCompat"
?d.body
:d.documentElement;
returna.clientHeight;
}
44.去掉url前缀
functionremoveUrlPrefix(a){
?a
:g.documentElement;
returnMath.max(f.scrollWidth,a.scrollWidth,d.clientWidth);
}
42.获取页面lTop
functiongetPageScrollTop(){
vara=document;
returna.documentElement.scrollTop||a.body.scrollTop;
varfixViewPortsExperiment=rendererModel.runningExperiments.FixViewport||rendererModel.runningExperiments.fixviewport;
varfixViewPortsExperimentRunning=fixViewPortsExperiment&&(fixViewPortsExperiment.toLowerCase()==="new");
}
39.获取页面scrollLeft
functiongetPageScrollLeft(){
vara=document;
returna.documentElement.scrollLeft||a.body.scrollLeft;
}
40.获取页面可视宽度
functiongetPageViewWidth(){
break
}
}
}
}
returnels
}
38.获取页面高度
functiongetPageHeight(){
varg=document,a=g.body,f=g.documentElement,d=patMode=="BackCompat"
?a
:g.documentElement;
returnMath.max(f.scrollHeight,a.scrollHeight,d.clientHeight);
returnfalse;
}
48.获取网页被卷去的位置
functiongetScrollXY(){
returndocument.body.scrollTop?{
x:document.body.scrollLeft,
y:document.body.scrollTop
}:{
x:document.documentElement.scrollLeft,
if(regular.test(strUrl)){
returntrue;
}else{
returnfalse;
}
}
37.getElementsByClassName
functiongetElementsByClassName(name){
vartags=document.getElementsByTagName('*')||document.all;
if(this.isAndroidMobileDevice()&&!this.isNewChromeOnAndroid()){
screenWidth=screenWidth/window.devicePixelRatio;
}
varFixViewPortsExperiment=rendererModel.runningExperiments.FixViewport||rendererModel.runningExperiments.fixviewport;
}
returnthis._initZoom;
}
34.获取移动设备最大化大小
functiongetZoom(){
varscreenWidth=(Math.abs(window.orientation)===90)?Math.max(screen.height,screen.width):Math.min(screen.height,screen.width);
functionchgCase(sStr,iCase){
if(typeofsStr!="string"||sStr.length<=0||!(iCase===0||iCase==1)){
returnsStr;
}
vari,oRs=[],iCode;
if(iCase){/*半->全*/
for(i=0;i<sStr.length;i+=1){
}
32.判断是否打开视窗
functionisViewportOpen(){
return!!document.getElementById('wixMobileViewport');
}
33.获取移动设备初始化大小
functiongetInitZoom(){
if(!this._initZoom){
varscreenWidth=Math.min(screen.height,screen.width);
varFixViewPortsExperimentRunning=FixViewPortsExperiment&&(FixViewPortsExperiment==="New"||FixViewPortsExperiment==="new");
if(FixViewPortsExperimentRunning){
y:document.documentElement.scrollTop
}
}
49.日期格式化函数+调用方法
Date.prototype.format=function(format){
varo={
"M+":this.getMonth()+1,//month
"d+":this.getDate(),//day
Javascript
在上一篇文章《Javascript常用方法函数收集(一)》中,作者分享了30个Javascript常用方法函数。在本文中,我们将继续上篇中的JS常用方法函数进行分享,希望能对学习前端的朋友们有所帮助。
31.判断是否Touch屏幕
functionisTouchScreen(){
return(('ontouchstart'inwindow)||window.DocumentTouch&&documentinstanceofDocumentTouch);
iCode=sStr.charCodeAt(i);
if(iCode==32){
iCode=12288;
}elseif(iCode<127){
iCode+=65248;
}
oRs.push(String.fromCharCode(iCode));
}
}else{/*全->半*/
for(i=0;i<sStr.length;i+=1){
if(iKey>95&&iKey<108){returntrue;}/*数字键盘1*/
if(iKey>108&&iKey<112){returntrue;}/*数字键盘2*/
if(iKey>185&&iKey<193){returntrue;}/*符号1*/
if(iKey>218&&iKey<223){returntrue;}/*符号2*/
}
47.确认是否键盘有效输入值
functioncheckKey(iKey){
if(iKey==32||iKey==229){returntrue;}/*空格和异常*/
if(iKey>47&&iKey<58){returntrue;}/*数字*/
if(iKey>64&&iKey<91){returntrue;}/*字母*/
if(fixViewPortsExperiment){
if(this.isAndroidMobileDevice()&&!this.isNewChromeOnAndroid()){
smallerSide=smallerSide/window.devicePixelRatio;
}
}
returnsmallerSide;
}
45.随机数时间戳
functionuniqueId(){
vara=Math.random,b=parseInt;
returnNumber(newDate()).toString()+b(10*a())+b(10*a())+b(10*a());
}
46.全角半角转换
//iCase: 0全到半,1半到全,其他不转化
varels=[];
for(vari=0;i<tags.length;i++){
if(tags.className){
varcs=tags.className.split(' ');
for(varj=0;j<cs.length;j++){
if(name==cs[j]){
els.push(tags);
if(this.isAndroidMobileDevice()&&!this.isNewChromeOnAndroid()){
screenWidth=screenWidth/window.devicePixelRatio;
}
this._initZoom=screenWidth/document.body.offsetWidth;
a=a.replace(/:/g,":").replace(/./g,".").replace(///g,"/");
while(trim(a).toLowerCase().indexOf("http://")==0){
a=trim(a.replace(/http:\/\//i,""));
}
returna;
}
36.完美判断是否为网址
functionIsURL(strUrl){
varregular=/^\b(((https?|ftp):\/\/)?[-a-z0-9]+(\.[-a-z0-9]+)*\.(?:com|edu|gov|int|mil|net|org|biz|info|name|museum|asia|coop|aero|[a-z][a-z]|((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d))\b(\/[-a-z0-9_:\@&?=+,.!\/~%\$]*)?)$/i
};
if(/(y+)/.test(format))format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4-RegExp.$1.length));
for(varkino){
if(newRegExp("("+k+")").test(format))