HTML怎么限制每行字数,JS实现每行固定字数,自动换行
JS實現每行固定字數,自動換行
autoWrap.html 原碼
.wrap_focus{border-width: 3px;border-style: solid;border-color:orange;width: 250px;height: 25px;font-size: 16px;font-weight: bolder;};
.wrap_blur{border-width:0 0 1px 0;border-style: solid;width: 250px;height: 25px;font-size: 16px;font-weight: bolder;};
style="width: 250px;height: 200px;overflow: auto;background-color: white;">
同步VALUE框
巴塞羅那4-1阿森納
var autoLength=20;//漢字作為2個字符計算
new autoWrap("beforeguide",autoLength);
///
autoWrap.js 原碼
var autoWrap=function(entityId,length){
this.divId="div."+entityId;//初始化外層DIVID
this.resultId="result."+entityId;//初始化同步結果TEXTID
this.entityId=entityId;//初始化默認TEXTID
this.length=length;//每行最長長度
this.split="/r/n";
this._init();
};
autoWrap.prototype.get=function(e){
return typeof(e)=='string'?document.getElementById(e):e;
};
autoWrap.prototype.addEvent=function(o,e,fn){
window.attachEvent ?o.attachEvent('on'+e,fn):o.addEventListener(e,fn,false);
};
autoWrap.prototype.removeEvent=function(o,e,fn){
window.detachEvent ?o.detachEvent('on'+e,fn):o.removeEventListener(e,fn,false);
};
autoWrap.prototype.wrapkeyup=function(e){//按鍵彈起事件
//判斷是否超長
var str=e.srcElement.value;
var num=0;
var wrap_i=0;
for(i=0;i
num++;
if(str.charCodeAt(i)>255)num++;//漢字作為2個字符計算
if(num>this.length)
{
wrap_i=i;
break;
}
}
if(wrap_i>0)//是否超長
e.srcElement.value=str.substring(0,wrap_i);
else
this.oldValue=str;
//數據同步處理
this.setResultValue(this.entityId);
}
autoWrap.prototype.wrap=function(e){
var id_end=e.srcElement.id.split("_")[1];//ID后綴
//
if(e.keyCode==13){//回車事件
var srcElement=this.get(this.entityId+"_"+id_end);
id_end++;//自動增加為下一個ID
var aElement=this.get(this.entityId+"_"+id_end);
if(aElement)//是否存在下一個,存在則使其獲得焦點
{
aElement.focus();
}
else//是否存在下一個,不存在則創建
{
aElement=document.createElement("");
aElement.id=this.entityId+"_"+id_end;
aElement.className="wrap_text";//初始化CSS
var self=this;
this.addEvent(aElement,'keydown',function(e){self.wrap(e)});
this.addEvent(aElement,'keyup',function(e){self.wrapkeyup(e)});
this.addEvent(aElement,'focus',function(e){self.wrapfocus(e)});
this.addEvent(aElement,'blur',function(e){self.wrapblur(e)});
this.div.appendChild(aElement);
aElement.focus();
}
}
//
if(e.keyCode==8&&e.srcElement.value.length==0){//回格事件
if(id_end>0)//判斷是否最初的一個
{
id_end--;//自動減少為上一個ID
var aElement=this.get(this.entityId+"_"+id_end);
aElement.focus();
var textRange=aElement.createTextRange();
textRange.collapse(false);
textRange.select();
this.div.removeChild(e.srcElement);
}
}
//
};
autoWrap.prototype.wrapfocus=function(e){
e.srcElement.className="wrap_focus";
}
autoWrap.prototype.wrapblur=function(e){
e.srcElement.className="wrap_blur";
}
autoWrap.prototype.setResultValue=function(arg){
var str=document.getElementsByName(arg)
var temp_str="";
for(i=0;i
{
if(i
temp_str+=str[i].value+"\r\n";
else
temp_str+=str[i].value;
}
this.result.value=temp_str;
};
//初始化默認值輸入框TEXT
autoWrap.prototype._initValue=function(){
//取得同步結果TEXT初始值,進行初始化
var guide=this.result.value;
if(guide.length>0)
{
var guide_list=guide.split(this.split);
for(i=0;i
{
if(i==0)
this.entity.value=guide_list[0];
if(i>0){
aElement=document.createElement("");
aElement.id=this.entityId+"_"+i;
aElement.value=guide_list[i];
aElement.className="wrap_blur";
var self=this; //初始化默認TEXT事件
this.addEvent(aElement,'keydown',function(e){self.wrap(e)});
this.addEvent(aElement,'keyup',function(e){self.wrapkeyup(e)});
this.addEvent(aElement,'focus',function(e){self.wrapfocus(e)});
this.addEvent(aElement,'blur',function(e){self.wrapblur(e)});
this.div.appendChild(aElement);
}
}
}
}
autoWrap.prototype._init=function(){
this.div=this.get(this.divId);//外層DIV
this.entity=this.get(this.entityId+"_0");//默認TEXT
this.result=this.get(this.resultId);//同步結果TEXT
this.entity.className="wrap_blur";//初始化默認TEXT樣式
if(typeof this.entity!='object'){
alert('entity is not object!');
return;
}
var self=this; //初始化默認TEXT事件
this.addEvent(this.entity,'keydown',function(e){self.wrap(e)});
this.addEvent(this.entity,'keyup',function(e){self.wrapkeyup(e)});
this.addEvent(this.entity,'focus',function(e){self.wrapfocus(e)});
this.addEvent(this.entity,'blur',function(e){self.wrapblur(e)});
this._initValue();
};
轉http://www.huomo.cn/developer/article-e280.html
來源:CSDN
作者:iteye_1744
鏈接:https://blog.csdn.net/iteye_1744/article/details/82507324
總結
以上是生活随笔為你收集整理的HTML怎么限制每行字数,JS实现每行固定字数,自动换行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android异步线程未执行,关于多线程
- 下一篇: html h1转换为行内,css中转换为