1、cookie操作;
function setCookie(name, value, iDay){var oDate = new Date;oDate.setDate(oDate.getDate() + iDay);document.cookie = name+'='+value+';expires='+oDate;}function getCookie(name){var arr=document.cookie.split('; ');for(var i=0;i
2、数字保留至少2位和小数点至少2位;
function iTime(num){if(num<10){return "0"+num;}else{return ""+num;}}/** * @param num 数字 * @param digit 小数点保留位数[默认2位] */function decimal(num,digit){digit = digit ? digit : 2;return new Number(num).toFixed(digit);}
3、点击收藏
$(function(){var ajaxCollectUrl = $('input[name=ajaxCollectUrl]').val();var loginUrl = $('input[name=loginUrl]').val();$('.collectBtn').click(function(){var _this = this;var type = $(this).attr('type'); //牌号:1 生产商:2 供应商:3var msg = $(this).attr('data-id');if( !type || !msg ) return;if( $(_this).hasClass('starIcon2') || $(_this).find('i').hasClass('starIcon2') ){var msg = "已收藏!";layer_comfirm3(msg);return;}$.post(ajaxCollectUrl, {type:type,msg:msg}, function(data){if( data.data == -1 ){ //未登陆window.location.href = loginUrl;}else if( data.data == -2 ){alert('收藏失败');}else if( data.data == 0 ){ //收藏成功if( $(_this).hasClass('starIcon') ){$(_this).addClass('starIcon2').removeClass('starIcon');}else if( $(_this).find('i').hasClass('starIcon')){$(_this).find('i').addClass('starIcon2').removeClass('starIcon');}var msg = "收藏成功!";layer_comfirm3(msg);}});});});function layer_comfirm3(msg){var is_click = false;layer_confirm2(msg);$('.close').click(function(){ is_click = true; layer.closeAll(); }); setTimeout(function(){ !is_click && layer.closeAll(); },2000);}
4、分类导航
$(function(){var timer = null;$('#sub,.oLeftBar').hover(function(){clearTimeout(timer);$('.oLeftBar').show();},function(){timer = setTimeout(function(){$('.oLeftBar').hide();}, 500);});$(".left-conList").hover(function(){ $(this).find(".leftBar-sub").show(); },function(){ $(this).find(".leftBar-sub").hide(); }); $(".leftBar-operation span").each(function(i){ $(this).click(function(){ $(".leftBar-con:eq("+i+")").fadeIn().siblings(".leftBar-con").hide(); //console.log($(".leftBar-con").length); }); $(".left-conList").hover(function(){ $(this).find(".leftBar-sub").show(); },function(){ $(this).find(".leftBar-sub").hide(); }); });
5、关键词筛选
/** * 关键词筛选 * @param key 关键词或字母 * @param type 搜索类型[1:字母分类筛选, 2:内容搜索] */function filterBykey(key, type){if( type == 2 ) $('.letters-box li a').eq(0).addClass('selected').parent().siblings('li').find('a').removeClass('selected');if( (type == 1 && key == '全部') || (type == 2 && key == '') ){$('#checkboxscroll a').show();return;}key = key.toUpperCase();$('#checkboxscroll a').each(function(){var akey= $.trim( $(this).attr('data-key') ).toUpperCase();if( type == 1 ){ //1:字母分类筛选key == akey ? $(this).show() : $(this).hide();}else{ //2:内容搜索var atext = $.trim( $('font', this).text() ).toUpperCase();//首字母匹配 | 文字匹配( (akey.indexOf(key) >= 0) || (atext.indexOf(key) >= 0) ) ? $(this).show() : $(this).hide();}});}
6、自制提示框
function myTooltip($obj, $text, $tipLeft, $tipTop){if( $('.tooltip.any').length > 0 ){$('.tooltip.any').fadeIn(300);setTimeout(function(){$('.tooltip.any').fadeOut(500)},2000);return;}$obj.parent().css({'position':'relative'});var objTop = $obj.position().top;var objLeft = $obj.position().left;var html = '
' +'
' + $text + '
';'
';$obj.after(html);var $tipLeft = $tipLeft ? $tipLeft : objLeft + Math.ceil($obj.width()/2) - Math.ceil($('.tooltip.any').width()/2)var $tipTop = $tipTop ? $tipTop : ($obj.height() + 22);$('.tooltip.any').css({'top': -( $tipTop ), 'left': $tipLeft}).fadeIn(500);setTimeout(function(){$('.tooltip.any').fadeOut(500)},2000);}
7、下拉菜单
function selectModel(){ var $box = $('div.model-select-box'); var $option = $('ul.model-select-option', $box); var $txt = $('div.model-select-text', $box); var speed = 10; $txt.click(function(e) { var $current = $(this).siblings('ul.model-select-option'); $option.not($current).slideUp(speed, function(){ int($(this)); }); $current.slideToggle(speed, function(){ int($(this)); }); return false; }); $option.find('li') .each(function(index, element) { if($(this).hasClass('selected')){ $(this).addClass('data-selected'); $(this).parent().siblings('.model-select-text').text( $(this).text() ); $(this).parent().siblings($('input.selected-input')).val( $(this).attr('data-option') ); } }) .mousedown(function(){ $(this).parent().siblings('div.model-select-text').text($(this).text()) .attr('data-value', $(this).attr('data-option')); $(this).parent().siblings($('input.selected-input')).val( $(this).attr('data-option') ); $(this).addClass('seleced data-selected').siblings('li').removeClass('seleced data-selected'); $option.slideUp(speed, function(){ //int($(this)); }); return false; }) .mouseover(function(){ $(this).addClass('seleced').siblings('li').removeClass('seleced'); }); $(document).click(function(e) { $option.slideUp(speed, function(){ int($(this)); }); }); function int(obj){ obj.find('li.data-selected').addClass('seleced').siblings('li').removeClass('seleced'); }}
8、验证码生成
function createVerifyCode(){codeImg = $('img.verifyCode');var verifyimg = codeImg.attr("src"); codeImg.attr('title', '点击刷新'); codeImg.click(function(){ if( verifyimg.indexOf('?')>0){ $(this).attr("src", verifyimg+'&random='+Math.random()); }else{ $(this).attr("src", verifyimg.replace(/?.*$/,'')+'?'+Math.random()); } });}
9、倒计时
function setTransactionClock(){var timeSpan = $('.headclock .timeSpan');if (timeSpan.length == 0) return;var type = $('.headclock').attr('type');var t = $('.headclock').attr('t');var text = type == 1 ? '距离开市:' : '闭市时间倒计时:';var timer = null;clearInterval(timer);timer = setInterval( function(){if( t <= 0 ){clearInterval( timer );window.location.reload(true);}str = ''+iTime(Math.floor(t%86400/3600))+' : ' + iTime(Math.floor(t%86400%3600/60)) + ' : ' + iTime(t%60) +'';timeSpan.html( text + str );t--;}, 1000);}
10、搜索
function searchWord(getSearchResUrl, jurl, reshref){var searchInput = $('div.seach>input.seachInput');var noInput="请输入品种/牌号/生产商进行搜索";var historyObj = $('.h_history');var searchRes = $('.search_res');var preSearchWord = ''; //记录上一次搜索内容searchInput.focus(function(){$(this).attr('placeholder','') if( $(this).val() == '' ){searchRes.hide();history(historyObj,searchInput);}else{historyObj.hide();getSearchRes( $(this).val() );}}).blur(function(){$(this).attr('placeholder',noInput);}).keydown(function(e){if(e.keyCode==13) $(this).siblings('button.seachBtn').click();}).keyup(function() {if( $(this).val() == '' ){searchRes.hide();history(historyObj,searchInput);}else{searchInput.val( $(this).val() ); //两个搜索框内容保持一致historyObj.hide();getSearchRes( $(this).val() );}});$('button.seachBtn').click(function(){historyObj.hide();var in_val = stripscript( $(this).siblings('input.seachInput').val() );if(!in_val) return false;//将搜索记录存入COOKIEvar inputHistory = $.cookie('inputHistory');if( typeof(inputHistory) != "undefined" ){inputHistory = inputHistory.split(",");if( $.inArray(in_val,inputHistory) == -1 ) inputHistory.push(in_val);}else inputHistory = in_val;$.cookie('inputHistory', inputHistory.toString(),{ path: "/"});jurl = jurl.replace('__', in_val);window.location.href = jurl;//console.log(jurl);});$(document).on('click',function(event){if( $(event.target).parents('.s_search_seek').length == 0 ){historyObj.hide();searchRes.hide();}});//历史记录function history(historyObj,searchInput){var inputHistory=$.cookie('inputHistory');var historyArr=[];if( typeof(inputHistory) != "undefined" ){ inputHistory = inputHistory.split(",");for(var j in inputHistory) inputHistory[j] && historyArr.push(inputHistory[j]);} if ( historyArr.length == 0 ) return; historyObj.children('li').not('.first').remove(); for( var i in historyArr){ historyObj.append( $(''+historyArr[i]+'删除')); } historyObj.show().children('li').hover(function(){ $(this).addClass('act').siblings('li').removeClass('act'); }); //删除历史记录 historyObj.find('li a').click(function(event){ if( $(this).parent().hasClass('first') ){ historyObj.hide(); $.cookie('inputHistory', '', { path: "/"}); }else{ $(this).parent().remove(); var index = $.inArray( $(this).siblings('span').text(), historyArr ); if(index>=0){ historyArr.splice(index,1); $.cookie('inputHistory', historyArr.toString(), { path: "/"}); } //只剩一条first if( historyObj.find('li').length == 1){ historyObj.hide(); } } event.stopPropagation(); event.preventDefault(); event.cancelBubble = true; }); historyObj.find('li span:gt(0)').click(function(){ searchInput.val( $(this).text() ); historyObj.hide(); searchRes.hide(); getSearchRes( $(this).text() ); });}//搜索关键词function getSearchRes(stext){stext = stripscript(stext);if( stext == '' ) history(historyObj,searchInput);else if( stext == preSearchWord ) (searchRes.find('res_left li').length>0) && searchRes.show();else{//searchRes.find('ul li').remove().end().find('.res_right dl').remove().end().show();$.post(getSearchResUrl,{'inputWord':stext},function(data){if( data.code != 0 ){ searchRes.hide(); return;}var data = data.msg;searchRes.find('ul li').remove().end().find('.res_right dl').remove().end().show();$(data).each(function(i,item){searchRes.find('ul').append( $(''+item.name+'') );var zysc_url = reshref.zysc.replace('__', item.name);var chsc_url = reshref.chsc.replace('__', item.name);var qgzx_url = reshref.qgzx.replace('__', item.name);var dl = '
' + ''+ item.name +'';if(item.zyscNum > 0) dl += '自营商城约'+ item.zyscNum +'条搜索结果';if(item.chscNum > 0) dl += '撮合市场约'+ item.chscNum +'条搜索结果';if(item.qgzxNum > 0) dl += '求购中心约'+ item.qgzxNum +'条搜索结果';//if(item.lxbNum > 0) dl += '料性表约'+ item.lxbNum +'条搜索结果';//if(item.zxdtNum >0) dl += '资讯动态约'+ item.zxdtNum +'条搜索结果';dl += ''searchRes.find('.res_right').append( $(dl) );//默认显示第一条searchRes.find('ul li').eq(0).addClass('act');searchRes.find('.res_right dl').eq(0).show();searchRes.find('ul li').hover(function(){var index = searchRes.find('ul li').index($(this));$(this).addClass('act').siblings('li').removeClass('act');searchRes.find('.res_right dl').eq(index).show().siblings('dl').hide();}).click(function(){ searchInput.val( $(this).text() ); searchRes.hide();});});},"json");}preSearchWord = stext;}}
11、校验输入域是否有特殊符号
function stripscript(s) {s = striphtml(s); var pattern = new RegExp("[`~!@#$^&*()=|{}':;',[].<>/?/~!@#¥……&*()&;—|{}【】‘;:”“'。,、?]"); var rs = ""; for (var i = 0; i < s.length; i++) { rs = rs + s.substr(i, 1).replace(pattern, ''); } return rs;}
12、过滤html变迁和换行回车等标记
function striphtml(string){string = string.replace(/()/g, ""); string = string.replace(/()/g, ""); string = string.replace(/()/g, ""); string = string.replace(/?[^>]*>/g, ""); string = string.replace(/s*/g, ""); string = string.replace(//g, ""); // return string;}
13、给数字添加toFixed方法
Number.prototype.toFixed = function(s) { var changenum = (parseInt(this * Math.pow(10, s) + 0.5) / Math.pow(10, s)).toString(); index = changenum.indexOf("."); if (index < 0 && s > 0) { changenum = changenum + "."; for (i = 0; i < s; i++) { changenum = changenum + "0"; } } else { index = changenum.length - index; for (i = 0; i < (s - index) + 1; i++) { changenum = changenum + "0"; } } return changenum; }