0

我自愿为一个小型组织开发这个脚本(作为我编程自我培训的一部分)。它随机选择房间中的一个座位(花几秒钟让观众猜测选择了哪个座位)。由于我使用的是 Mac,所以我主要使用 Firefox 进行测试,它的工作原理很吸引人。结果他们房间里的电脑在 Windows XP 上运行 Internet Explorer 6(他们必须在本地文件中启用活动内容)。

他们打电话通知我一个错误,根据他们提供的信息,有罪的路线是RowNumberDisplay.style = "color: #FF0000";var RowNumberDisplay = document.getElementById("RowNumber");。在 Google 和 Stack Overflow 上快速搜索 IE6 中的 getElementById 和 .style 问题没有结果(一个常见的问题是由于名称属性导致的错误匹配,但有问题的 div 没有名称属性)。提前感谢您提供任何有助于识别和解决此错误的答案。

<!doctype html>
<html>
    <head>
        <title>The Lucky Person</title>
            <style type="text/css">
            input#SelectLuckyPerson
            {
                height: 150px;
                font-size: 60px;
            }
            p#RowDetails, p#SeatDetails, div#RowNumber, div#SeatNumber
            {
                font-size: 100px;
                display: inline;
            }
            div#RowNumber, div#SeatNumber
            {
                color: #0000FF;
            }
            </style>
    </head>
    <body>
        <div id="LuckyPersonIs"><input type="button" id="SelectLuckyPerson" value="And the lucky person is..." onClick="GetResults();"></div>
        <p id="RowDetails">Row number: <div id="RowNumber">0</div></p>
        <p id="SeatDetails">Seat number: <div id="SeatNumber">0</div></p>
        <script>
            var MinRow = 2;
            var MaxRow = 8;
            var SeatsInRow = new Array();
            SeatsInRow[1] = 25;
            SeatsInRow[2] = 25;
            SeatsInRow[3] = 27;
            SeatsInRow[4] = 27;
            SeatsInRow[5] = 27;
            SeatsInRow[6] = 27;
            SeatsInRow[7] = 29;
            SeatsInRow[8] = 31;
            SeatsInRow[9] = 31;
            SeatsInRow[10] = 31;
            SeatsInRow[11] = 31;
            SeatsInRow[12] = 33;
            SeatsInRow[13] = 33;
            SeatsInRow[14] = 33;
            var ShuffleSpeed = 200;
            var RowNumberDisplay = document.getElementById("RowNumber");
            var SeatNumberDisplay = document.getElementById("SeatNumber");
            var ChosenRow, ChosenSeat
            function GetResults()
            {
                var IsRunning = CheckStatus();
                if (IsRunning)
                {
                    ChosenRow = ChooseRow();
                    ChosenSeat = ChooseSeat();
                    RowNumberDisplay.style = "color: #FF0000";
                    SeatNumberDisplay.style = "color: #FF0000";
                    ShowRowResult = window.setInterval("TryRowResult()", ShuffleSpeed);
                    if (DelaySeats == false)
                    {
                        ShowSeatResult = window.setInterval("TrySeatResult()", ShuffleSpeed);
                    }
                }
            }
            function ChooseRow()
            {
                return Math.floor(Math.random() * (MaxRow - MinRow)) + MinRow;
            }
            function ChooseSeat()
            {
                return Math.ceil(Math.random() * SeatsInRow[ChosenRow]);
            }
            function TryRowResult()
            {
                TryRow = ChooseRow();
                RowNumberDisplay.innerHTML = TryRow;
                if (TryRow == ChosenRow)
                {
                    window.clearInterval(ShowRowResult);
                    document.getElementById("RowNumber").style = "color: #0000FF";
                    if (DelaySeats == true)
                    {
                        ShowSeatResult = window.setInterval("TrySeatResult()", ShuffleSpeed);
                    }
                }
            }
            function TrySeatResult()
            {
                TrySeat = ChooseSeat();
                SeatNumberDisplay.innerHTML = TrySeat;
                if (TrySeat == ChosenSeat)
                {
                    window.clearInterval(ShowSeatResult);
                    document.getElementById("SeatNumber").style = "color: #0000FF";
                }
            }
            function CheckStatus()
            {
                if (RowNumberDisplay.style.color == "rgb(255, 0, 0)" || SeatNumberDisplay.style.color == "rgb(255, 0, 0)")
                {
                    return false;
                }
                return true;
            }
            </script>
    </body>
</html>
4

1 回答 1

5

元素的style属性是一个对象,而不是字符串。要将文本分配给它,就像您使用 CSS 属性一样,您应该分配给element.style.cssText,这将为您解析出字符串,并充当定义多种样式的快捷方式。

但是,在这种情况下,您可以使用它:

ChosenRow.style.color = ChosenSeat.style.color = "#f00";
于 2013-10-03T01:25:00.453 回答