获取链接URL的参数 var key=getQueryStringByKey('参数名称')
function getQueryStringByKey(key) { var returnValue = ""; var str = window.location.search.toLowerCase(); if ($.trim(str).length == 0) { returnValue = ""; } else { str = str.substr(1); var strArr = str.split("&"); for (var i = 0; i < strArr.length; i++) { var index = strArr[i].indexOf("="); var keyStr = strArr[i].substring(0, index); if (key.toLowerCase() == keyStr) { returnValue = strArr[i].substr(index + 1); break; } } } return returnValue; }
保留数字的几位小数:
function getFormatedNum(num, n) { //参数说明:num 要格式化的数字 n 保留小数位 // num = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); return Number(num).toFixed(n); }
自己写String.Format
在javascript、Jquery里面好像是没有String.format();这个函数的,所以我们在拼接字符串的时候就特别的辛苦,生怕又打错,而且又乱,所以就自己去写一个函数来代替。 String.format = function() { if (arguments.length == 0) return null; var str = arguments[0]; for ( var i = 1; i < arguments.length; i++) { var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm'); str = str.replace(re, arguments[i]); } return str; }; // var a = "我喜欢吃{0},也喜欢吃{1},但是最喜欢的还是{0},偶尔再买点{2}"; // alert(String.format(a, "苹果","香蕉","香梨")); // 结果:我喜欢吃苹果,也喜欢吃香蕉,但是最喜欢的还是苹果,偶尔再买点香梨 是从0位开始的。
对URL进行编码
javascript自带的URL编码函数:encodeURI(string)
给html表格加排序
首先用css加上上下箭头的文字图标
.icon-xiajiantou:before { content: "\e646"; } .icon-shangxia:before { content: "\e648"; } .icon-shangxia1:before { content: "\e64e"; }
然后写上JS插件:
(function ($) { $.fn.extend({ //插件名称 - paddingList SortTable: function (callback) { var allth = $(this).find("th"); $(allth).find("i").remove(); $(allth).each(function () { var sequence = parseInt($(this).attr("data-sequence")); if (sequence != -1) { $(this).css({ "cursor": "pointer" }); $(this).append('<i class="iconfont icon-shangxia table-head-iconstyle"></i>'); } }); return this.find("th").each(function () { var sequence = parseInt($(this).attr("data-sequence")); if (sequence != -1) { $(this).on("click", function () { var sortData = thClick($(this), allth); if (callback) { console.log(sortData); callback(sortData); } }) } }); } }); })(jQuery); //sequence:0:默认;1:升序;2:降序 function thClick(thisTh, allTh) { var sortData = { "field": "", "direction": "" }; var direction = ""; var field = thisTh.attr("data-field"); var sequence = parseInt(thisTh.attr("data-sequence")); if (isNaN(sequence)) { sequence = 0; } sequence++; sequence = sequence > 2 ? 0 : sequence; thisTh.attr("data-sequence", sequence); if (sequence == 0) { $(thisTh).find(".table-head-iconstyle").addClass("icon-shangxia").removeClass("icon-xiajiantou").removeClass("icon-shangxia1"); } else if (sequence == 1) { $(thisTh).find(".table-head-iconstyle").removeClass("icon-shangxia").removeClass("icon-xiajiantou").addClass("icon-shangxia1"); direction = "asc"; } else if (sequence == 2) { $(thisTh).find(".table-head-iconstyle").removeClass("icon-shangxia").removeClass("icon-shangxia1").addClass("icon-xiajiantou"); direction = "desc"; } $.each(allTh, function () { if (field != $(this).attr("data-field")) { $(this).find(".table-head-iconstyle").addClass("icon-shangxia").removeClass("icon-xiajiantou").removeClass("icon-shangxia1"); } }) if (direction != "") { sortData.field = field; sortData.direction = direction; } return sortData; }
最后就可以使用啦:$.SortTable(function(){})