document.getElementById(...)为null或不是对象....但它是?

时间:2013-01-13 16:18:25

标签: javascript arrays getelementbyid xhtml-transitional

嘿所以我有点困惑为什么我收到错误,'document.getElementById(...)'为null或不是对象。我是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>

非常感谢您提供的任何帮助!

6 个答案:

答案 0 :(得分: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"
     ...
];

答案 1 :(得分:1)

你需要报价。

document.getElementById('foo').innerHTML

答案 2 :(得分:1)

尝试将'foo'放在引号中:

document.getElementById('foo')

答案 3 :(得分:1)

您必须使用引号标识id,所以:

document.getElementById('foo').innerHTML

答案 4 :(得分:1)

首先看一下,我看到r_card [51] =“黑桃王”实际上应该是r_card [51] =“黑桃之王”; 和var i = Math.floor(52 * Math.random())应该是var i = Math.floor(52 * Math.random());

那些讨厌的人很重要。

答案 5 :(得分:0)

你忘了把Id放进“......”

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

我认为这应该有用