方法名 说明 使用

charAt(index) 返回指定位置的字符 str.charAt(0)
charCodeAt(index) 获取指定位置处字符的ASCII码 str.charCodeAt(0)
str[index] 获取指定位置处字符 HTML5,IE8+支持和chaAt()等效

  1. <script><br /> //1.charAt(index) 根据位置返回字符串<br /> var str = 'andy';<br /> console.log(str.charAt(3));<br /> console.log('----------------');<br /> //遍历所有的字符<br /> for(i = 0; i < str.length; i++){<br /> console.log(str.charAt(i));<br /> }<br /> console.log('----------------');<br /> //2.charCodeAt(index) 获取指定位置处字符的ASCII码 目的:判断用户按的哪个键<br /> console.log(str.charCodeAt(0));//97<br /> console.log('----------------');<br /> //3.str[index] H5新增的<br /> console.log(str[0]);<br /> </script>