20

数字を文字に変換しようとしています。数字または数字と文字のいずれかが必要な div の配列を作成しています。したがって、1-3 は 1-3 です。ただし、4-13 は a/4、b/5、c6 などである必要があります。これらの数字を簡単に文字に変換できる方法はありますか? ASCII値を一定量変更することはできますか?

     for(var i = 1; i < 33; i++){
    if( i < 4 || (i > 13 && i < 20) || i > 29){
        $('#teeth-diagram').append("<div class='tooth' id='" + i + "'>&nbsp;</div>");
    }else{
        $('#teeth-diagram').append("<div class='tooth' id='" + Letter goes here + "/" + i + "'>&nbsp;</div>");
    }
}
4

3 に答える 3

47

since 97 is the ascii value for 'a', and your value for 'a' is 3, you need to do this to get the value of the integer converted to a character:

if(i>=3){
    String.fromCharCode(94 + i);
}
于 2012-11-02T19:49:02.087 に答える
26

Yes, you can. Use var letter = String.fromCharCode(number); To get a lowercase a, the number would be 97, b would be 98 and so forth. For uppercase A 65, B would be 66 and so forth. See this JSFiddle for an example

于 2012-11-02T19:48:53.560 に答える