定義:
charAt() 方法可返回指定位置的字符。
JavaScript 並沒有一種有別於字符串類型的字符數據類型,所以返回的字符是長度爲 1 的字符串。
語法:
stringObject.charAt(index)
TIY
<html>
<body>
<script type="text/javascript">
var str="Hello world!"
document.write("The first character is: " + str.charAt(0) + "<br />")
document.write("The second character is: " + str.charAt(1) + "<br />")
document.write("The third character is: " + str.charAt(2))
</script>
</body>
</html>
顯示
The first character is: H
The second character is: e
The third character is: l
應用:
因爲charAt()方法只能截取一個字符,所以要想獲取姓名後兩位只能拼接起來。
var applyUserId = data.data.rows[i].userName;//獲取姓名
var namesmall = applyUserId.charAt(applyUserId.length - 2) + applyUserId.charAt(applyUserId.length - 1); // 截取姓名後兩位
顯示: