• js实现限制容器中字符个数


    html:

    <div class="ellipsis">
        夜空中最亮的星/请指引我走出去/夜空中最亮的星 是否知道/那曾与我同心的身影 如今在哪里/夜空中最亮的星 是否在意/
    </div>

    js:

    $(document).ready(function(){
    //限制字符个数
    $(".ellipsis").each(function(){
    var maxwidth=39;
    if($(this).text().length>maxwidth){
    $(this).text($(this).text().substring(0,maxwidth));
    $(this).html($(this).html()+'…');
    alert();
    }
    });
    });

    另一种:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>输入字符限制</title>
            <script type="text/javascript" src="js/jquery-1.11.0.js"></script>
            <style>
                .tucao_dl {
                  width: 518px;
                  overflow: hidden;
                }
                .tucao_dl dt {
                  clear: both;
                  margin-top: 5px;
                }
                .tucao_dl dt textarea {
                  background: none;
                  border: none;
                  border: 1px solid #ddd;
                  width: 510px;
                  height: 135px;
                  line-height: 24px;
                  padding-top: 3px;
                  font-size: 14px;
                  color: #999;
                  font-family: '微软雅黑';
                  resize: none;
                }
                .tucao_dl dt i {
                  color: #ff6600;
                }
                .tucao_dl dd {
                  float: left;
                  color: #999;
                  font-family: '微软雅黑';
                  font-size: 14px;
                }
                .tucao_dl dd i {
                  color: #ff6600;
                }
                .focusing{background-color: rgba(22,219,10,0.5);}
                
    
            </style>
        </head>
        <body>
    
            <dl class="tucao_dl" >
               <dt>
                   <textarea id="OtherText" title="http://www.cnblogs.com/Han39/。" class="feedbackT" onkeyup="maxlength(this)" onfocus="editAdvice()"></textarea>
               </dt>
               <dd>您还可以输入<i>200</i></dd>
           </dl>
    
        </body>
        <script>
            $(document).ready(function(){
                $('#OtherText').each(function(){
                    $(this).val($(this).attr('title'));
                }).focus(function(){
                    if(this.value == $(this).attr('title')){
                        $(this).val('').addClass('focusing');
                    };
                    
                }).blur(function(){
                    if(this.value == ''){
                        $(this).val($(this).attr('title')).removeClass('focusing');
                    };
                });
    
            });
            function maxlength(obj){;
                var num = $(obj).val().length;
                if(200-num>=0){
                    $('.tucao_dl dd').find('i').text(200-num);
                }
                obj.value = obj.value.slice(0, 200);
            }
            function editAdvice(){
                  var advice=removeAllSpace($("#OtherText").val());
                  var title=removeAllSpace($("#OtherText").attr("title"));
                  if(advice==null ||advice.length<=0 || advice ==title){
                    $("#OtherText").text("");
                  }
                
            }
        </script>
    </html>
  • 相关阅读:
    2016.6.13 php与MySQL数据库交互之数据库中的商品信息展示
    2016.6.12 计算机网络复习重点第二章之信道复用技术
    2016.6.10 计算机网络复习要点第二章物理层
    2016.6.11 ASP提交数据到SQL server数据乱码解决方法
    2016.6.6 计算机网络考试要点第一章之网络体系结构
    2016.6.5 计算机网络考试要点第一章之计算机网络性能
    tornado之Hello world
    Python执行show slave status输出的两个格式
    Python logging模块
    Python正则表达式指南(转)
  • 原文地址:https://www.cnblogs.com/Han39/p/7207012.html
Copyright © 2020-2023  润新知