用js實(shí)現(xiàn)計(jì)算代碼行數(shù)的簡(jiǎn)單方法附代碼
更新時(shí)間:2007年08月13日 19:59:54 作者:
一段代碼可能有幾十行,上千個(gè)字符,統(tǒng)計(jì)其行數(shù)可以先把代碼字符串化為數(shù)組,再返回該數(shù)組的長(zhǎng)度即可。
傳統(tǒng)做法:
<textarea name="abc">
123456
789
</textarea>
<script type="text/javascript">
var counter = 0;
var str = abc.value;
for(i=0; i<str.length; i++)
{
if(str.substr(i,1)=="\n")
counter +=1;
}
alert(counter);
</script>
轉(zhuǎn)為數(shù)組后效率就高了很多:
<textarea name="abc">
123456
789
</textarea>
<script type="text/javascript">
alert(abc.value.split("\n").length)
</script>
<textarea name="abc" style="width:480px; height:220px;">
system.println("hello world!")
alert("hello world!");
messagebox_ok("hello world")
msgbox("hello world!")
</textarea>
<script type="text/javascript">
alert("代碼總行數(shù):"+(abc.value.split("\n").length-1).toString())
</script>
傳統(tǒng)做法:
<textarea name="abc">
123456
789
</textarea>
<script type="text/javascript">
var counter = 0;
var str = abc.value;
for(i=0; i<str.length; i++)
{
if(str.substr(i,1)=="\n")
counter +=1;
}
alert(counter);
</script>
轉(zhuǎn)為數(shù)組后效率就高了很多:
<textarea name="abc">
123456
789
</textarea>
<script type="text/javascript">
alert(abc.value.split("\n").length)
</script>
<textarea name="abc" style="width:480px; height:220px;">
system.println("hello world!")
alert("hello world!");
messagebox_ok("hello world")
msgbox("hello world!")
</textarea>
<script type="text/javascript">
alert("代碼總行數(shù):"+(abc.value.split("\n").length-1).toString())
</script>
您可能感興趣的文章:
- 用JS獲得表格當(dāng)前行數(shù)的代碼
- js動(dòng)態(tài)生成指定行數(shù)的表格
- js獲取表格的行數(shù)和列數(shù)的方法
- 基于JavaScript實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建表格和增加表格行數(shù)
- javascript實(shí)現(xiàn)輸出指定行數(shù)正方形圖案的方法
- php實(shí)現(xiàn)格式化多行文本為Js可用格式
- 利用javascript實(shí)現(xiàn)禁用網(wǎng)頁(yè)上所有文本框,下拉菜單,多行文本域
- 用原生js統(tǒng)計(jì)文本行數(shù)的簡(jiǎn)單示例
相關(guān)文章
js抽獎(jiǎng)轉(zhuǎn)盤(pán)實(shí)現(xiàn)方法分析
這篇文章主要介紹了js抽獎(jiǎng)轉(zhuǎn)盤(pán)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了js抽獎(jiǎng)轉(zhuǎn)盤(pán)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
javascript:以前寫(xiě)的xmlhttp池,代碼
用javascript寫(xiě)的xmlhttp池代碼,最下面有調(diào)用方法2008-05-05
js實(shí)現(xiàn)帶緩沖效果的仿QQ面板折疊菜單代碼
這篇文章主要介紹了js實(shí)現(xiàn)帶緩沖效果的仿QQ面板折疊菜單代碼,通過(guò)JavaScript定時(shí)函數(shù)遞歸調(diào)用實(shí)現(xiàn)折疊菜單的緩沖效果,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
JavaScript使ifram跨域相互訪問(wèn)及與PHP通信的實(shí)例
這篇文章主要介紹了JavaScript使ifram跨域相互訪問(wèn)及與PHP通信的實(shí)例,同時(shí)對(duì)同域間的訪問(wèn)也作了詳細(xì)的演示,需要的朋友可以參考下2016-03-03
JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)端播放攝像頭實(shí)時(shí)畫(huà)面
這篇文章主要介紹了如何利用JavaScript實(shí)現(xiàn)在網(wǎng)頁(yè)端播放局域網(wǎng)(不能上云)或是廣域網(wǎng)的攝像頭的實(shí)時(shí)畫(huà)面,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02

