写给朋友用的,回调提供剩余字符
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <style type="text/css"> .out{ background-color: #ccc; opacity: 0; filter:alpha(opacity=0); position: relative; z-index: 3 } .out img{ position: relative; } </style> </head> <body> <input type="text" class="limit" /> <div class="out"> <img src="http://172.16.102.228:3000/images/html/ac_game/ac_game_pair_back_answer1.png" /> <img src="http://172.16.102.228:3000/images/html/ac_game/ac_game_pair_back_base.png" /> </div> <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script type="text/javascript"> ;(function($){ $.fn.maxLimit = function(options){ var defaults = { max : 11, callback : null }, configs = $.extend({},defaults,options), self = this, max = configs.max, callback = configs.callback, _length, Val, getLength = function(x){ // 中文算2个字符 return x.replace(/[^\x00-\xf]/g,"xx").length; } self.on("input propertychange",function(){ var val = self.val(), length = getLength(val); if(length > max){ self.val(Val); }else{ Val = val; } if(typeof callback == "function"){ callback.call(null, max - getLength(Val) ); } }); } })(jQuery); $(".limit").maxLimit({ max : 7, callback : function(l){ console.log(l); } }); </script> </body> </html>
时间: 2024-10-17 01:56:56