编辑文本后点击显示结果
返回网页教程首页
<html> <head> <title>用length属性得到字符串长度的Javascript示例</title> </head> <body> <script type="text/javascript"> var vText="I like aspxhome.com/chm/html" document.write("<p>" + vText + "</p>") document.write(vText.length) </script> <p>Javascript示例代码解释:首先声明一个变量vText,将"I like aspxhome.com/chm/html"赋值给vText,然后用document.write将其显示出来;然后用length属性,计算得出vText的长度,并将其显示出来,vText里面包含16个字符,空格也是计算在内的,最后得出结果就是16。如果你写中文,就会得出中文字符的长度。比如你写,"我就喜欢asp之家",就会得到这个字符串的长度,是8。 </p> </body> </html>