超实用的JavaScript代码段 附使用方法
JavaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。
本文为大家整理了5段实用JavaScript代码,便于大家进行开发。
1.判断日期是否有效
JavaScript中自带的日期函数还是太过简单,很难满足真实项目中对不同日期格式进行解析和判断的需要。JQuery也有一些第三方库来使日期相关的处理变得简单,但有时你可能只需要一个非常简单的函数,而不想引入一个庞大的第三方库。这时,你可以使用下面这段日期校验代码,它允许你自定义日期格式并进行日期有效性的校验。
functionisValidDate(value,userFormat){ //Setdefaultformatifformatisnotprovided userFormat=userFormat||'mm/dd/yyyy'; //Findcustomdelimiterbyexcluding //month,dayandyearcharacters vardelimiter=/[^mdy]/.exec(userFormat)[0]; //Createanarraywithmonth,dayandyear //soweknowtheformatorderbyindex vartheFormat=userFormat.split(delimiter); //Createarrayfromuserdate vartheDate=value.split(delimiter); functionisDate(date,format){ varm,d,y,i=0,len=format.length,f; for(i;i<len;i++){ f=format[i]; if(/m/.test(f))m=date[i]; if(/d/.test(f))d=date[i]; if(/y/.test(f))y=date[i]; } return( m>0&&m<13&& y&&y.length===4&& d>0&& //Checkifit'savaliddayofthemonth d<=(newDate(y,m,0)).getDate() ); } returnisDate(theDate,theFormat); }
使用方法:
下面这个调用返回false,因为11月份没有31天
isValidDate('dd-mm-yyyy','31/11/2012')
2.获取一组元素的最大宽度或高度
下面这个函数,对于需要进行动态排版的开发人员非常有用。
vargetMaxHeight=function($elms){ varmaxHeight=0; $elms.each(function(){ //InsomecasesyoumaywanttouseouterHeight()instead varheight=$(this).height(); if(height>maxHeight){ maxHeight=height; } }); returnmaxHeight; };
使用方法:
$(elements).height(getMaxHeight($(elements)));
3.高亮文本
有很多JQuery的第三方库可以实现高亮文本的功能,但我更喜欢用下面这一小段JavaScript代码来实现这个功能,它非常短小,而且可以根据我的需要去进行灵活的修改,而且可以自己定义高亮的样式。下面这两个函数可以帮助你创建自己的文本高亮插件。
functionhighlight(text,words,tag){ //Defaulttagifnotagisprovided tag=tag||'span'; vari,len=words.length,re; for(i=0;i<len;i++){ //Globalregextohighlightallmatches re=newRegExp(words[i],'g'); if(re.test(text)){ text=text.replace(re,'<'+tag+'class="highlight">$&</'+tag+'>'); } } returntext; }
你同样会需要取消高亮的函数:
functionunhighlight(text,tag){ //Defaulttagifnotagisprovided tag=tag||'span'; varre=newRegExp('(<'+tag+'.+?>|<\/'+tag+'>)','g'); returntext.replace(re,''); }
使用方法:
$('p').html(highlight( $('p').html(),//thetext ['foo','bar','baz','helloworld'],//listofwordsorphrasestohighlight 'strong'//customtag ));
4.文字动效
有时你会希望给你的一段文字增加动效,让其中的每个字都动起来。你可以使用下面这段jQuery插件代码来达到这个效果。当然你需要结合一个CSS3transition样式来达到更好的效果。
$.fn.animateText=function(delay,klass){ vartext=this.text(); varletters=text.split(''); returnthis.each(function(){ var$this=$(this); $this.html(text.replace(/./g,'<spanclass="letter">$&</span>')); $this.find('span.letter').each(function(i,el){ setTimeout(function(){$(el).addClass(klass);},delay*i); }); }); };
使用方法:
$('p').animateText(15,'foo');
5.逐个隐藏元素
下面这个jQuery插件可以根据你设置的步长(间隔时间)来逐个隐藏一组元素。在列表元素的重新加载中使用,可以达到很好的效果。
$.fn.fadeAll=function(ops){ varo=$.extend({ delay:500,//delaybetweenelements speed:500,//animationspeed ease:'swing'//otherrequireeasingplugin },ops); var$el=this; for(vari=0,d=0,l=$el.length;i<l;i++,d+=o.delay){ $el.eq(i).delay(d).fadeIn(o.speed,o.ease); } return$el; }
使用方法:
$(elements).fadeAll({delay:300,speed:300});
以上只是那些实用JavaScript代码段中的一小部分,希望对大家学习javascript程序设计有所帮助。