/*
创建日期:2008-09-24
创 建 人:SHREK*/
(function($){
/*
功能说明:
返回所需长度的字符串
如果输入的字符串超出长度,则从右侧截取定长度
参数说明:
value 要进行格式化的字符串
length 需要的长度
opts 设定用的JSON
subdirection 当超长时,才有用。
0 右侧向左侧截取,
其他值 左侧向右侧
lackfill 不足所需长度时用于左侧填充的字符/字符串(默认为空格)
exceedfill 超长时在后面补充的字符串(默认为0长度字符串)
*/
$.toFixedWidth = function(value, length, opts){
opts = jQuery.extend({
subdirection: "0",
lackfill: ' ',
exceedfill: ''
},
opts || {});
var result = value.toString();
var padding = length - result.length;
if(padding < 0){
if(opts.subdirection == 0){
result = result.substr(-padding);
result = opts.exceedfill + result;
}
else{
result = result.substr(0, length);
result = result + opts.exceedfill;
}
}
else{
for(var n = 0; n < padding; n++){
result = opts.lackfill + result;
}
}
return result;
};
/*
全角字符截取
参数说明参见toFixedWidth
*/
$.toFixedWidthForWideAngle = function(value, length, opts){
opts = jQuery.extend({
subdirection: "0",
lackfill: ' ',
exceedfill: ''
},
opts || {});
if(length == 0){
return "";
}
length = length * 2;
var result = value.toString();
function getSourceStringLength(sourcestr){
var valLength = 0;
for(var i = 0; i < sourcestr.length; i++){
var code = escape(sourcestr.charAt(i));
if(code.length >= 4 && (code < '%uFF60' || code > '%uFF9F')){
valLength += 2;
}
else{
valLength += 1;
}
}
return valLength;
}
function isWideAngle(chr){
if(code.length >= 4 && (code < '%uFF60' || code > '%uFF9F')){
return true;
}
else{
return false;
}
}
var resultWideLength = getSourceStringLength(result)
var padding = length - resultWideLength;
if(padding < 0){
var tmpresult = "";
var curpositon = 0;
if(opts.subdirection == 0){
for(var i = result.length - 1; i >= 0; i++){
var code = escape(result.charAt(i));
if(code.length >= 4 && (code < '%uFF60' || code > '%uFF9F')){
curpositon += 2;
}
else{
curpositon += 1;
}
if(curpositon > length) break;
tmpresult = result.charAt(i) + tmpresult;
}
}
else{
for(var i = 0; i < result.length; i++){
var code = escape(result.charAt(i));
if(code.length >= 4 && (code < '%uFF60' || code > '%uFF9F')){
curpositon += 2;
}
else{
curpositon += 1;
}
if(curpositon > length) break;
tmpresult += result.charAt(i);
}
}
result = tmpresult;
}
else{
for(var n = 0; n < padding; n++){
result = opts.lackfill + result;
}
}
return result;
};
/*
功能说明:
返回queryString中对应的值
参数说明
opts
key 要查找的键
defaultvalue 不存在返回的值
*/
$.queryString = function(opts){
opts = jQuery.extend({
key: "?",
defaultValue: null
},
opts || {});
var thisPageUrl = location.search.toLowerCase();
var key = opts.key.toLowerCase();
//没有的时候返回默认值
if(thisPageUrl.length == 0){
return opts.defaultValue;
}
var iPos = thisPageUrl.indexOf("?" + key + "=");
if(iPos == -1){
iPos = thisPageUrl.indexOf("&" + key + "=");
if(iPos == -1){
return opts.defaultValue;
}
}
var tmpStr = thisPageUrl.substr(iPos + key.length + 2, thisPageUrl.length - (iPos + key.length + 2));
var iPos2 = tmpStr.indexof("&");
if(iPos2 == -1){
return tmpStr;
}
else{
tmpStr = tmpStr.substr(0, iPos2);
}
return tmpStr;
}
})(jQuery);
使用方法:
$.toFixedWidthForWideAngle('a哈哈c','1',{subdirection: 1,exceedfill:"我超过了"});
$.queryString({key:'aa', defaultValue:'我是默认值'})