在Javascript中自动增量字母表

时间:2016-11-21 18:03:16

标签: javascript

任何人都可以帮我解决这个问题吗?我在javascript中创建了一个自动增量字母,但如果单击按钮则没有任何反应。



function myFunction() {
    var test = 5;
    var b = String.fromCharCode(64 + parseInt(test.toString()));
    var a = String.fromCharCode(64 + 1);

    for (var i = a; i <= b.length; i++) {
        document.getElementById("demo").innerHTML = text[i];
    };
}
&#13;
 <button onclick="myFunction()">Try it</button>

<p id="demo"></p>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

对于初学者,您的代码中未定义text

&#13;
&#13;
var currentCode = 0;

function nextChar() {
    currentCode++;
    var character = String.fromCharCode(64 + currentCode);
 
    document.getElementById("demo").innerHTML = character;
}

nextChar(); // Initialize with "A"
&#13;
<button onclick="nextChar()">Try it</button>

<p id="demo"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是我对你的尝试

&#13;
&#13;
function myFunction() {
    for (i = 65; i < 91; i++) {
        var letter = String.fromCharCode(i);
        document.write(letter);
    }
}
&#13;
<button onclick="myFunction()">Try it</button>
&#13;
&#13;
&#13;