用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)文章
javascript中this做事件參數(shù)相關(guān)問(wèn)題解答
有關(guān)this想必大家早有所耳聞,只不過(guò)在使用中不是那么在意而已,接下來(lái)為大家介紹下this做事件參數(shù)問(wèn)題,感興趣的你可不要錯(cuò)過(guò)了哈2013-03-03JavaScript事件學(xué)習(xí)小結(jié)(五)js中事件類型之鼠標(biāo)事件
這篇文章主要介紹了JavaScript事件學(xué)習(xí)小結(jié)(五)js中事件類型之鼠標(biāo)事件的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06