源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

仿微博字符限制效果实现代码

  • 时间:2022-04-12 14:59 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:仿微博字符限制效果实现代码
这是初始状态 [img]http://files.jb51.net/upload/201204/20120420010336220.jpg[/img]   输入文字变成这样,这里会区分圆角半角,2个半角的文字算一个。 [img]http://files.jb51.net/upload/201204/20120420010336522.jpg[/img]   这个是超出的样子 [img]http://files.jb51.net/upload/201204/20120420010336170.jpg[/img]   如果超出了点击提交,会有红色闪动提示 [img]http://files.jb51.net/upload/201204/20120420010336281.jpg[/img]   好了,效果就是这样子,都是js的。。用的话,记得加个jq文件过来。。 这里是超出只有提示,还可以超出以后截掉多余的。。不过公司项目不用,说是体验不好~~~
[u]复制代码[/u] 代码如下:
var oH2 = $("#spetit_word");//提示文字 var oTextarea = $("#p_qa_content");//输入框 var oButton = $("#bt-ico");//按钮
[u]复制代码[/u] 代码如下:
oTextarea.live("keyup", function () { Limit(oTextarea, 280, oH2); }) oButton.live("click", function () { if (font_count < 0 || font_count == null || font_count == 140) { Error(oTextarea); } else { alert('发布成功!'); } });
[u]复制代码[/u] 代码如下:
var font_count; function WordLength(obj) { var oVal = obj.val(); var oValLength = 0; oVal.replace(/\n*\s*/, '') == '' ? oValLength = 0 : oValLength = oVal.match(/[^ -~]/g) == null ? oVal.length : oVal.length + oVal.match(/[^ -~]/g).length; return oValLength } function Error(obj) { var oTimer = null; var i = 0; oTimer = setInterval(function () { i++; i == 5 ? clearInterval(oTimer) : (i % 2 == 0 ? obj.css("background-color", "#ffffff") : obj.css("background-color", "#ffd4d4")); }, 100); } //obj-要检查的输入框, iNow-多少字, tit-提示框 function Limit(obj, iNow, tit) { var oValLength = WordLength(obj); font_count = Math.floor((iNow - oValLength) / 2); if (font_count >= 0) { tit.html("你还可以输入<strong>" + font_count + "</strong>字"); return true; } else { tit.html("已超出<strong style='color:red'>" + Math.abs(font_count) + "</strong>字"); return false; } return font_count; }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部