0

私は JS と HTML にかなり慣れていません (約 20 時間前に開始)、すでに問題があります: 以下に私のコードを示します。あるチュートリアルが言ったように、ボタンをクリックすると statusLine テキストが変更されます。しかし、何かがうまくいかず、私はそれを理解できません。

    <!DOCTYPE html>
<html>
<head>
<title>Некое подземелье</title>
</head>
<body>
<p id="statusLine">Вы попали в подземелье.</p>

<button type="button" onclick="goDeeper()">Идти глубже в подземелье</button>

<script>
    function goDeeper()
     {
       var nextEvent=(Math.floor(Math.random()*10+1));
       switch(nextEvent){
        case'1':
            document.getElementById("statusLine").innerHTML="Вам на пути попался гоблин!";
            break;
                }
     }
</script>
</body>
</html>

だから、何かが間違っています。これを修正するにはどうすればよいですか?

4

2 に答える 2

3

case ステートメントを1文字列ではなく数値に一致させてみて'1'ください。

function goDeeper()
{
    var nextEvent = Math.floor(Math.random()*10+1);
    switch(nextEvent) {
        case 1:
            document.getElementById("statusLine").innerHTML="Вам на пути попался гоблин!";
            break;
    }
}

さらに言えば、一致させる必要がある条件が 1 つしかない場合は、 を削除しswitchて単純なifブロックを使用します。

function goDeeper()
{
    var nextEvent = Math.floor(Math.random()*10+1);
    if (nextEvent == 1) {
        document.getElementById("statusLine").innerHTML="Вам на пути попался гоблин!";
    }
}
于 2013-11-03T20:25:39.793 に答える
0
What I understand you want to change the text on click button so Try this 

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="Hello World";
}
</script>
</head>
<body>

<p>Click the button to trigger a function.</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>`enter code here`

</body>
</html>

Refrence Link : http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick
于 2013-11-03T20:30:13.330 に答える