0

'document.getElementById(...)' is null or not an object. 私はJavaScriptが初めてで、配列の作成を練習しています。このスクリプトは、私が単純に持っていた前にうまく機能しdocument.write(r_card[i]);、 no function. ページが読み込まれ、カードがランダムに選択されます。しかし、関数と呼び出しを練習したかったので、そのfunction bar() {部分を追加してに変更document.write(r_card[i]);しましdocument.getElementById(foo).innerHTML = (r_card[i]);たが、今は機能していません。自分で JavaScript を書くのは初めてなので、通常、このような問題に遭遇すると構文エラーが発生します。いくつかの検索を行いましたが、同じエラーを持つ人を見つけることができませんでした。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <title>Pick a Card!</title>
</head>

<body>
    <script type="text/javascript">
        function bar() {
            var r_card = new Array();
            r_card[0] = "Ace of Diamonds";
            r_card[1] = "Two of Diamonds";
            r_card[2] = "Three of Diamonds";
            r_card[3] = "Four of Diamonds";
            r_card[4] = "Five of Diamonds";
            r_card[5] = "Six of Diamonds";
            r_card[6] = "Seven of Diamonds";
            r_card[7] = "Eight of Diamonds";
            r_card[8] = "Nine of Diamonds";
            r_card[9] = "Ten of Diamonds";
            r_card[10] = "Jack of Diamonds";
            r_card[11] = "Queen of Diamonds";
            r_card[12] = "King of Diamonds";
            r_card[13] = "Ace of Clubs";
            r_card[14] = "Two of Clubs";
            r_card[15] = "Three of Clubs";
            r_card[16] = "Four of Clubs";
            r_card[17] = "Five of Clubs";
            r_card[18] = "Six of Clubs";
            r_card[19] = "Seven of Clubs";
            r_card[20] = "Eight of Clubs";
            r_card[21] = "Nine of Clubs";
            r_card[22] = "Ten of Clubs";
            r_card[23] = "Jack of Clubs";
            r_card[24] = "Queen of Clubs";
            r_card[25] = "King of Clubs";
            r_card[26] = "Ace of Hearts";
            r_card[27] = "Two of Hearts";
            r_card[28] = "Three of Hearts";
            r_card[29] = "Four of Hearts";
            r_card[30] = "Five of Hearts";
            r_card[31] = "Six of Hearts";
            r_card[32] = "Seven of Hearts";
            r_card[33] = "Eight of Hearts";
            r_card[34] = "Nine of Hearts";
            r_card[35] = "Ten of Hearts";
            r_card[36] = "Jack of Hearts";
            r_card[37] = "Queen of Hearts";
            r_card[38] = "King of Hearts";
            r_card[39] = "Ace of Spades";
            r_card[40] = "Two of Spades";
            r_card[41] = "Three of Spades";
            r_card[42] = "Four of Spades";
            r_card[43] = "Five of Spades";
            r_card[44] = "Six of Spades";
            r_card[45] = "Seven of Spades";
            r_card[46] = "Eight of Spades";
            r_card[47] = "Nine of Spades";
            r_card[48] = "Ten of Spades";
            r_card[49] = "Jack of Spades";
            r_card[50] = "Queen of Spades";
            r_card[51] = "King of Spades"
            var i = Math.floor(52 * Math.random())
            document.getElementById(foo).innerHTML = (r_card[i]);
        }
    </script>
    <input type="button" value="Pick a Card!" onclick="bar()" />
    <br />
    <div id="foo">Test</div>
</body>

</html>

あなたが提供できるどんな助けも大歓迎です!

4

6 に答える 6

5

交換

document.getElementById(foo).innerHTML = (r_card[i]);

document.getElementById('foo').innerHTML = (r_card[i]);

という名前の変数がありませんfoo

補足として、配列宣言をよりきれいにすることができます

var r_card = [
     "Ace of Diamonds",
     "Two of Diamonds"
     ...
];
于 2013-01-13T16:20:40.890 に答える
1

引用符が必要です。

document.getElementById('foo').innerHTML
于 2013-01-13T16:21:58.373 に答える
1

「foo」を引用符で囲んでみてください。

document.getElementById('foo')
于 2013-01-13T16:22:14.727 に答える
1

id を引用符で識別する必要があるため、次のようになります。

document.getElementById('foo').innerHTML
于 2013-01-13T16:22:19.047 に答える
1

r_card[51] = "King of Spades" は実際には r_card[51] = "King of Spades" である必要があります。および var i = Math.floor(52 * Math.random()) は var i = Math.floor(52 * Math.random()); である必要があります。

それらの厄介な;は重要です。

于 2013-01-13T16:24:04.120 に答える
0

「...」にIDを入れるのを忘れました

            var i = Math.floor(52 * Math.random())
            document.getElementById("foo").innerHTML = (r_card[i]);

これはうまくいくはずだと思います

于 2013-01-13T16:34:32.953 に答える