JS实现每行固定字数,自动换行
autoWrap.html 原码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
.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>
<script type="text/javascript" src="autoWrap.js"></script>
</head>
<body>
<div id="div.beforeguide"
style="width: 250px;height: 200px;overflow: auto;background-color: white;">
<input type="text" name="beforeguide" id="beforeguide_0">
</div>
同步VALUE框<br>
<textarea rows="10" cols="25" name="result.beforeguide" id="result.beforeguide" readonly="readonly">巴塞罗那4-1阿森纳</textarea>
<script type="text/javascript">
var autoLength=20;//汉字作为2个字符计算
new autoWrap("beforeguide",autoLength);
</script>
</body>
</html>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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<str.length;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("<input type='text' name='"+this.entityId+"'>");
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<str.length;i++)
{
if(i<str.length-1)
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<guide_list.length;i++)
{
if(i==0)
this.entity.value=guide_list[0];
if(i>0){
aElement=document.createElement("<input type='text' name='"+this.entityId+"'>");
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
- 大小: 20.9 KB
分享到:
相关推荐
9.5 表格的宽度固定后内容自动换行 9.6 表格的排序 9.7 表格的斜线 9.8 table中的文字滚动 9.9 JavaScript遍历table的行和列 9.10 表格按回车自动生成新行 9.11 单击单元格背景变色 9.12 单击表格某行后其他行隐藏 ...
9.5 表格的宽度固定后内容自动换行 9.6 表格的排序 9.7 表格的斜线 9.8 table中的文字滚动 9.9 JavaScript遍历table的行和列 9.10 表格按回车自动生成新行 9.11 单击单元格背景变色 9.12 单击表格某行后其他行隐藏 ...
1.21 实现类Twitter的字数限制效果 1.22 提示文本的隐藏与显示 1.23 实现文字闪烁效果 1.24 实现文字动画效果 1.25 实现文字跟随鼠标移动变化的动画效果 1.26 文本域中光标的定位 1.27 实现可折叠效果 1.28 文本框...
1. **设置固定宽度**:你可以为`<select>`元素设置一个固定的宽度,确保它不会因为内容过长而扩大。例如: ```html ;"> <!-- 选项内容 --> ``` 这种方法适用于选项长度大致相同的情况,但可能无法应对所有...
在本文中,我们将探讨如何使用JavaScript和CSS技术实现当网页中的文本内容超出设定的长度时,自动用省略号(...)来代替超出部分,并且当用户将鼠标悬停在该文本上时,显示完整的文本信息。这通常是在网页设计中用来...