JS給Textarea文本框添加行號(hào)的方法
本文實(shí)例講述了JS給Textarea文本框添加行號(hào)的方法。分享給大家供大家參考。具體如下:
這里使用JS實(shí)現(xiàn)讓Textarea文本框顯示行號(hào)的功能,每一行的前面都會(huì)有下數(shù)字序號(hào),如果用來(lái)顯示代碼的話,可以直接找到某一行,如果不顯示行號(hào),則還要自己手功去查,想要此功能,你只需設(shè)置好TextArea ID,并加入代碼中的JavaScript代碼部分即可,文本框的長(zhǎng)寬則是由CSS來(lái)控制的,你可試著修改一下,長(zhǎng)寬的顯示要與JS相匹配。
運(yùn)行效果截圖如下:

在線演示地址如下:
http://demo.jb51.net/js/2015/js-textarea-show-row-num-codes/
具體代碼如下:
<html>
<head>
<title>Js給文本框添加行號(hào)功能</title>
<style type="text/css">
#codeTextarea{width: 500px;height: 310px;}
.textAreaWithLines{font-family: courier;border: 1px solid #ddd;}
.textAreaWithLines textarea,.textAreaWithLines div{border: 0px;line-height: 120%;font-size: 12px;}
.lineObj{color: #666;}
</style>
<script type="text/javascript">
var lineObjOffsetTop = 2;
function createTextAreaWithLines(id)
{
var el = document.createElement('DIV');
var ta = document.getElementById(id);
ta.parentNode.insertBefore(el,ta);
el.appendChild(ta);
el.className='textAreaWithLines';
el.style.width = (ta.offsetWidth + 30) + 'px';
ta.style.position = 'absolute';
ta.style.left = '30px';
el.style.height = (ta.offsetHeight + 2) + 'px';
el.style.overflow='hidden';
el.style.position = 'relative';
el.style.width = (ta.offsetWidth + 30) + 'px';
var lineObj = document.createElement('DIV');
lineObj.style.position = 'absolute';
lineObj.style.top = lineObjOffsetTop + 'px';
lineObj.style.left = '0px';
lineObj.style.width = '27px';
el.insertBefore(lineObj,ta);
lineObj.style.textAlign = 'right';
lineObj.className='lineObj';
var string = '';
for(var no=1;no<20;no++){
if(string.length>0)string = string + '<br>';
string = string + no;
}
ta.onkeydown = function() { positionLineObj(lineObj,ta); };
ta.onmousedown = function() { positionLineObj(lineObj,ta); };
ta.onscroll = function() { positionLineObj(lineObj,ta); };
ta.onblur = function() { positionLineObj(lineObj,ta); };
ta.onfocus = function() { positionLineObj(lineObj,ta); };
ta.onmouseover = function() { positionLineObj(lineObj,ta); };
lineObj.innerHTML = string;
}
function positionLineObj(obj,ta)
{
obj.style.top = (ta.scrollTop * -1 + lineObjOffsetTop) + 'px';
}
</script>
</head>
<body>
<form>
<textarea id="codeTextarea"></textarea>
</form>
<script type="text/javascript">
createTextAreaWithLines('codeTextarea');
</script>
</body>
</html>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- 關(guān)于js對(duì)textarea換行符的處理方法淺析
- jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法
- 把textarea中字符串里含有的回車(chē)換行替換成<br>的javascript代碼
- JS簡(jiǎn)單限制textarea內(nèi)輸入字符數(shù)量的方法
- JS實(shí)現(xiàn)選擇TextArea內(nèi)文本的方法
- js監(jiān)聽(tīng)鼠標(biāo)事件控制textarea輸入字符串的個(gè)數(shù)
- JS實(shí)現(xiàn)textarea通過(guò)換行或者回車(chē)把多行數(shù)字分割成數(shù)組并且去掉數(shù)組中空的值
相關(guān)文章
Javascript 顏色漸變效果的實(shí)現(xiàn)代碼
在搭建博主博客的時(shí)候,尋思著做一些效果,看到菜單,就想是不是可以做一下顏色的漸變,增加一點(diǎn)動(dòng)態(tài)的感覺(jué)。有個(gè)jquery的插件,效果相當(dāng)不錯(cuò),不過(guò)博主還是打算自立更生寫(xiě)一下,看看能不能實(shí)現(xiàn)2013-10-10
關(guān)于JS控制代碼暫停的實(shí)現(xiàn)方法分享
關(guān)于JS控制代碼暫停的工作總結(jié),需要的朋友可以參考下2012-10-10
LayUI數(shù)據(jù)接口返回實(shí)體封裝的例子
今天小編就為大家分享一篇LayUI數(shù)據(jù)接口返回實(shí)體封裝的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
使用js實(shí)現(xiàn)按鈕控制文本框加1減1應(yīng)用于小時(shí)+分鐘
正如標(biāo)題所言使用js實(shí)現(xiàn)按鈕控制文本框加1減1,此類(lèi)主要應(yīng)用于小時(shí)+分鐘,下面有個(gè)不錯(cuò)的示例,喜歡的朋友可以參考下2013-12-12
完美實(shí)現(xiàn)js選項(xiàng)卡切換效果(二)
這篇文章主要為大家詳細(xì)介紹如何完美實(shí)現(xiàn)js選項(xiàng)卡切換效果,通過(guò)設(shè)置定時(shí)器實(shí)現(xiàn)延時(shí)0.5s切換選項(xiàng)卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
echarts餅圖labelLine線的長(zhǎng)度自適應(yīng)設(shè)置
這篇文章主要給大家介紹了關(guān)于echarts餅圖labelLine線的長(zhǎng)度自適應(yīng)設(shè)置的相關(guān)資料,在echarts中,餅圖的標(biāo)簽線可以通過(guò)設(shè)置 labelLine屬性來(lái)自定義位置,需要的朋友可以參考下2023-08-08
javascript基于DOM實(shí)現(xiàn)權(quán)限選擇實(shí)例分析
這篇文章主要介紹了javascript基于DOM實(shí)現(xiàn)權(quán)限選擇的方法,實(shí)例分析了javascript針對(duì)頁(yè)面元素的動(dòng)態(tài)選擇與添加刪除等操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05

