zoukankan      html  css  js  c++  java
  • 用jquery控制span显示几个字符

    <script src="js/wordLimit.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function () {
    word();
    }); 
    function word(){
    $(".table").wordLimit(8);
    }
    </script>
    <body>
    <span class="table">测试就将计就计啦啦啦经济</span>
    </body>
    

      调用WordLimit.js如下:

    /* $(".test1").wordLimit(); 自动获取css宽度进行处理,如果css中未对.test1给定宽度,则不起作用 
        $(".test2").wordLimit(24); 截取字符数,值为大于0的整数,这里表示class为test2的标签内字符数最多24个 
    */  
      
    (function($){  
        $.fn.wordLimit = function(num){   
            this.each(function(){     
                if(!num){  
                    var copyThis = $(this.cloneNode(true)).hide().css({  
                        'position': 'absolute',  
                        'width': 'auto',  
                        'overflow': 'visible'  
                    });   
                    $(this).after(copyThis);  
                    if(copyThis.width()>$(this).width()){  
                        $(this).text($(this).text().substring(0,$(this).text().length-4));  
                        $(this).html($(this).html()+'...');  
                        copyThis.remove();  
                        $(this).wordLimit();  
                    }else{  
                        copyThis.remove(); //清除复制  
                        return;  
                    }     
                }else{  
                    var maxwidth=num;  
                    if($(this).text().length>maxwidth){  
                        $(this).text($(this).text().substring(0,maxwidth));  
                        $(this).html($(this).html()+'...');  
                    }  
                }                      
            });  
        }           
    })(jQuery);  
  • 相关阅读:
    redis配置认证密码以及远程访问
    Mybatis缓存配置
    Node.Js安装
    1.ArcGis几何图形之几何计算
    MessagePack 新型序列化反序列化方案
    Redis(六)-数据类型
    Redis(五)-数据库
    Redis(四)-配置
    Redis(三)-Ubuntu下安装
    Redis(二)-Win系统下安装
  • 原文地址:https://www.cnblogs.com/hyql/p/5344897.html
Copyright © 2011-2022 走看看