根据位置返回字符

 charAt(index)   返回指定位置的字符(index 字符串你的索引号)  str.charAt(0)
    charCodeAt(index)  获取指定位置处字符的ASCII码(index索引号)  str.charCodeAt(0)
    str[index]  过去指定位置处字符   HTML5,IE8+支持  和charAt( )等效
    1.charAt(index) 根据位置返回字符
  <script>
    var str = "andy";
    console.log(str.charAt(3));
    // 遍历所有的字符
    for (var i = 0; i < str.length; i++) {
      console.log(str.charAt(i));
    }
    //2.charCodeAt(index)  返回相应索的引号的字符ASCII值   目的:  判断用户按下了那个键
    console.log(str.charCodeAt(0)); //97
    //3.str[index]   H5新增的
    console.log(str[0]);
  </script>

 

posted @ 2022-04-13 09:34  罗砂  阅读(74)  评论(0)    收藏  举报