几个简单的jQuery扩展函数

/*

创建日期: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:'我是默认值'})

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值