>  기사  >  웹 프론트엔드  >  입력 및 텍스트 영역 입력 상자의 최대 텍스트 제한을 위한 jquery 플러그인

입력 및 텍스트 영역 입력 상자의 최대 텍스트 제한을 위한 jquery 플러그인

WBOY
WBOY원래의
2016-05-16 18:00:06980검색
코드 복사 코드는 다음과 같습니다.

/* 입력 및 텍스트 영역 최대 텍스트 제한 플러그인
* 수정된 버전, 한 중국어는 한 단어, 한 영어는 반 단어를 나타냅니다.
* TextLimit - 입력 및 텍스트 영역 필드의 문자 수를 계산하고 제한하는 jQuery 플러그인
*
* '-1'을 다음과 같이 전달합니다. 문자 삭제 효과를 원하지 않으면 속도를 높이세요. (0만 입력하지 마세요.)
* 예: jQuery("Textarea").textlimit('span.counter',256)
*
* $Version : 2009.07.25 r2
* Copyright (c) 2009 Yair Even-Or
* vsync.design@gmail.com
*/

String.prototype.getBytes = function () {
var cArr = this.match(/[^x00-xff]/ig);
return this.length (cArr == null ? 0 : cArr.length);
( function(jQuery) {
jQuery.fn.textlimit=function(counter_el, thelimit, speed) {
var charDelSpeed ​​​​= 속도 || 15;
varggleCharDel = 속도 != - 1;
varggleTrim = true;
var isCtrl = false;
updateCounter(){
if(typeof that == "object" )
jQuery(counter_el).text(thelimit - Math.ceil(that.value.getBytes()/2))
}
this.keydown(function(e ){
if (e.which == 17) isCtrl = true;
var ctrl_a = (e.which == 65 && isCtrl == true) ? // CTRL A 선택을 감지하고 허용합니다. all.
var ctrl_v = (e.which == 86 && isCtrl == true) ? true : false; // CTRL V 붙여넣기를 감지하고 허용합니다.
// 8은 '삭제'입니다. '
if( this.value.length >= thelimit && e.which != '8' && e.which != '46' && ctrl_a == false && ctrl_v == false)
e.preventDefault ();
})
.keyup (function(e){
updateCounter();
if(e.which == 17)
isCtrl=false;
if( this.value.length > ;= thelimit &&ggleTrim ){
if(toggleCharDel){
// 먼저 문자 자르기에 시간이 오래 걸리지 않도록 텍스트를 약간 자릅니다.
// 다음 사항도 확인하세요. 10개가 넘는 추가 문자가 있는 경우
if ( (this.value.length - thelimit) > 10 )
that.value = that.value.substr(0,thelimit 100 );
var init = setInterval
(
function(){
if( that.value.length <= thelimit ){
init =clearInterval(init); updateCounter()
}
else{
// 추가 문자 삭제(하나씩)
that.value = that.value.substring(0,that.value.length-1); text(Math.ceil( that.value.getBytes()/2));
}
} ,charDelSpeed ​​​​
)
}
else this.value = that.value .substr(0,thelimit);
}
})(jQuery);
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.