0

嘿,所以我有点困惑为什么我会收到错误,'document.getElementById(...)' 为空或不是对象。我是 javascript 新手并练习编写数组。这个脚本在我只是有document.write(r_card[i]);并且没有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 回答