森欄
2019-11-11 14:39:03
我想提醒一個字符串的每個字母,但是我不確定該怎么做。所以,如果我有:var str = 'This is my string';我希望能夠分別警告T,h,i,s等。這僅僅是我正在研究的一個想法的開始,但是我需要知道如何分別處理每個字母。我想使用jQuery,并考慮在測試字符串的長度后可能需要使用split函數(shù)。有想法嗎?
3 回答

冉冉說
TA貢獻1877條經(jīng)驗 獲得超1個贊
如果警報的順序很重要,請使用以下命令:
for (var i = 0; i < str.length; i++) {
alert(str.charAt(i));
}
如果警報的順序無關(guān)緊要,請使用以下命令:
var i = str.length;
while (i--) {
alert(str.charAt(i));
}

白衣染霜花
TA貢獻1796條經(jīng)驗 獲得超10個贊
這可能遠不止于此。只想貢獻另一個簡單的解決方案:
var text = 'uololooo';
// With ES6
[...text].forEach(c => console.log(c))
// With the `of` operator
for (const c of text) {
console.log(c)
}
// With ES5
for (var x = 0, c=''; c = text.charAt(x); x++) {
console.log(c);
}
// ES5 without the for loop:
text.split('').forEach(function(c) {
console.log(c);
});
添加回答
舉報
0/150
提交
取消