jQuery实现微博字数统计

XHTML
<form id="myform" action="say.php" method="post"> 
   <h3><span class="counter">140</span>说说你正在做什么...</h3> 
   <textarea name="saytxt" id="saytxt" class="input" rows="2" cols="40"></textarea> 
   <p> 
      <input type="image" src="images/btn.gif" class="sub_btn" alt="发布" /> 
      <span id="msg"></span> 
   </p> 
</form> 
<div class="clear"></div> 
<div id="saywrap"> 
    <div class="saylist"> 
       <a href="#"><img src="images/user.gif" alt="" /></a> 
       <div class="saytxt"> 
          <p><strong><a href="#">Demo</a></strong>发布的内容...</p> 
           <div class="date"></div> 
       </div> 
       <div class="clear"></div> 
    </div> 
</div> 

XHTML是一个表单,里面有输入框textarea,发布按钮,还有一个统计输入字数的span#counter,和信息提示span#msg,在没有输入的情况下就提交则会提示用户要求输入内容。

CSS
h3{height:32pxline-height:32pxfont-size:18pxh3 span{float:rightfont-size:32pxfont-family:Georgia,serif; color:#cccoverflow:hidden.input{width:594pxheight:58pxmargin:5px 0 10px 0padding:4px 2px;  
border:1px solid #aaafont-size:12pxline-height:18pxoverflow:hidden.sub_btn{float:rightwidth:94pxheight:28px;} 
#msg{color:#f30.clear{clear:both.saylist{margin:8px autopadding:4px 0border-bottom:1px dotted #d3d3d3.saylist img{float:leftwidth:50pxmargin:4px.saytxt{float:rightwidth:530pxoverflow:hidden.saytxt p{line-height:18px.saytxt p strong{margin-right:6px.date{color:#999

 

jQuery

先引入jquery库和global.js文件:

<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript" src="js/global.js"></script> 

global.js文件:

global.js要做的事有:

1、用户输入、鼠标离开输入框时,统计输入的字符数,并根据输入字数的不同而输出不同的样式(字体颜色)显示在页面上。

2、处理提交数据:当点击“发布”按钮时,显示等待图片,通过ajax想后台提交输入的数据,等待后台处理,并将处理结果输出给前端页面。

具体代码如下:

function recount(){ 
    var maxlen=140; 
    var current = maxlen-$('#saytxt').val().length; 
    $('.counter').html(current); 
 
    if(current<1 || current>maxlen){ 
        $('.counter').css('color','#D40D12'); 
        $('input.sub_btn').attr('disabled','disabled'); 
    } 
    else 
        $('input.sub_btn').removeAttr('disabled'); 
 
    if(current<10) 
        $('.counter').css('color','#D40D12'); 
 
    else if(current<20) 
        $('.counter').css('color','#5C0002'); 
 
    else 
        $('.counter').css('color','#cccccc'); 
 
} 

函数recount()完成了输入字符的统计,并根据输入的字符数,显示不同的字体颜色。

$(function(){ 
    $('#saytxt').bind("blur focus keydown keypress keyup"function(){ 
        recount(); 
    }); 
    $("#myform").submit(function(){ 
        var saytxt = $("#saytxt").val(); 
        if(saytxt==""){ 
            $("#msg").show().html("你总得说点什么吧.").fadeOut(1200);; 
            return false; 
        } 
        $('.counter').html('<img style="padding:8px" src="images/load.gif" alt="正在处理..." />'); 
        $.ajax({ 
           type: "POST", 
           url: "submit.php", 
           data:"saytxt="+saytxt, 
           dataType: "html", 
           success: function(msg){ 
              if(parseInt(msg)!=0){ 
                 $('#saywrap').prepend(msg); 
                 $('#saytxt').val(''); 
                 recount(); 
             } 
          } 
        }); 
        return false; 
    }); 
}); 

提交数据给后台后,由submit.php进行处理,关于后台的处理程序,我在下一篇文章会重点讲解,敬请关注。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值