网页程序设计之JavaScript代码段

时间:2022-08-24 03:11:07 JavaScript 我要投稿
  • 相关推荐

网页程序设计之实用JavaScript代码段

  JavaScript正变得越来越流行,它已经成为前端开发的第一选择。下面是小编收集的10段实用JavaScript代码,基于它们你还可以创造出更强大的JS插件或功能函数。

  1. 判断日期是否有效

  JavaScript中自带的日期函数还是太过简单,很难满足真实项目中对不同日期格式进行解析和判断的需要。JQuery也有一些第三方库来使日期相关的处理变得简单,但有时你可能只需要一个非常简单的函数,而不想引入一个庞大的第三方库。这时,你可以使用下面这段日期校验代码,它允许你自定义日期格式并进行日期有效性的校验。

  function isValidDate(value, userFormat) {

  // Set default format if format is not provided

  userFormat = userFormat || 'mm/dd/yyyy';

  // Find custom delimiter by excluding

  // month, day and year characters

  var delimiter = /[^mdy]/.exec(userFormat)[0];

  // Create an array with month, day and year

  // so we know the format order by index

  var theFormat = userFormat.split(delimiter);

  // Create array from user date

  var theDate = value.split(delimiter);

  function isDate(date, format) {

  var m, 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 &&

  // Check if it's a valid day of the month

  d <= (new Date(y, m, 0)).getDate()

  );

  }

  return isDate(theDate, theFormat);

  }

  使用方法:

  下面这个调用返回false,因为11月份没有31天

  isValidDate('dd-mm-yyyy', '31/11/2012')

  2. 获取一组元素的最大宽度或高度

  下面这个函数,对于需要进行动态排版的开发人员非常有用。

  var getMaxHeight = function ($elms) {

  var maxHeight = 0;

  $elms.each(function () {

  // In some cases you may want to use outerHeight() instead

  var height = $(this).height();

  if (height > maxHeight) {

  maxHeight = height;

  }

  });

  return maxHeight;

  };

  使用方法:

  $(elements).height( getMaxHeight($(elements)) );

  3. 高亮文本

  有很多JQuery的第三方库可以实现高亮文本的功能,但我更喜欢用下面这一小段JavaScript代码来实现这个功能,它非常短小,而且可以根据我的需要去进行灵活的修改,而且可以自己定义高亮的样式。下面这两个函数可以帮助你创建自己的文本高亮插件。

  function highlight(text, words, tag) {

  // Default tag if no tag is provided

  tag = tag || 'span';

  var i, len = words.length, re;

  for (i = 0; i < len; i++) {

  // Global regex to highlight all matches

  re = new RegExp(words[i], 'g');

  if (re.test(text)) {

  text = text.replace(re, '<'+ tag +'>$&');

  }

  }

  return text;

  }

  你同样会需要取消高亮的函数:

  function unhighlight(text, tag) {

  // Default tag if no tag is provided

  tag = tag || 'span';

  var re = new RegExp('(<'+ tag +'.+?>|<\/'+ tag +'>)', 'g');

  return text.replace(re, '');

  }

  使用方法:

  $('p').html( highlight(

  $('p').html(), // the text

  ['foo', 'bar', 'baz', 'hello world'], // list of words or phrases to highlight

  'strong' // custom tag

  ));

  4. 文字动效

  有时你会希望给你的一段文字增加动效,让其中的每个字都动起来。你可以使用下面这段jQuery插件代码来达到这个效果。当然你需要结合一个CSS3 transition样式来达到更好的效果。

  $.fn.animateText = function(delay, klass) {

  var text = this.text();

  var letters = text.split('');

  return this.each(function(){

  var $this = $(this);

  $this.html(text.replace(/./g, '$&'));

  $this.find('span.letter').each(function(i, el){

  setTimeout(function(){ $(el).addClass(klass); }, delay * i);

  });

  });

  };

  使用方法:

  $('p').animateText(15, 'foo');

  5. 逐个隐藏元素

  下面这个jQuery插件可以根据你设置的步长(间隔时间)来逐个隐藏一组元素。在列表元素的重新加载中使用,可以达到很好的效果。