// 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); functionisDate(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 <= (newDate(y, m, 0)).getDate() ); } returnisDate(theDate, theFormat); }
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; };
functionhighlight(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 = newRegExp(words[i], 'g'); if (re.test(text)) { text = text.replace(re, '<'+ tag +' class="highlight">$&'); } } return text; }
你同样会需要取消高亮的函数:
1 2 3 4 5 6
functionunhighlight(text, tag) { // Default tag if no tag is provided tag = tag || 'span'; var re = newRegExp('(<'+ tag +'.+?>|<\/'+ tag +'>)', 'g'); return text.replace(re, ''); }
使用方法:
1 2 3 4 5
$('p').html( highlight( $('p').html(), // the text ['foo', 'bar', 'baz', 'hello world'], // list of words or phrases to highlight 'strong'// custom tag ));
functionisBreakPoint(bp) { // The breakpoints that you set in your css var bps = [320, 480, 768, 1024]; var w = $(window).width(); var min, max; for (var i = 0, l = bps.length; i < l; i++) { if (bps[i] === bp) { min = bps[i-1] || 0; max = bps[i]; break; } } return w > min && w <= max; }
使用方法:
1 2 3 4 5 6
if ( isBreakPoint(320) ) { // breakpoint at 320 or less } if ( isBreakPoint(480) ) { // breakpoint between 320 and 480 }
$(element) .data('counter', 0) // begin counter at zero .click(function() { var counter = $(this).data('counter'); // get $(this).data('counter', counter + 1); // set // do something else... });
9. 嵌入优酷视频
1 2 3 4 5 6 7 8
functionembedYouku(link, ops) { var o = $.extend({ width: 480, height: 320, params: '' }, ops); var id = /\?v\=(\w+)/.exec(link)[1]; return'<embed id="embedid" src="'+id+'?'+o.ops'" type="application/x-shockwave-flash" width="'+o.width+'" height="'+o.height+'" align="middle"></embed>
functionmakeMenu(items, tags) { tags = tags || ['ul', 'li']; // default tags var parent = tags[0]; var child = tags[1]; var item, value = ''; for (var i = 0, l = items.length; i < l; i++) { item = items[i]; // Separate item and value if value is present if (/:/.test(item)) { item = items[i].split(':')[0]; value = items[i].split(':')[1]; } // Wrap the item in tag items[i] = '<'+ child +' '+ (value && 'value="'+value+'"') +'>'+ // add value if present item +''; } return'<'+ parent +'>'+ items.join('') +''; }