使用Javascript更改td元素文本 [英] Changing td element text using Javascript

查看:117
本文介绍了使用Javascript更改td元素文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为html文件编写代码,该代码将显示36000个掷骰子的每个骰子总数.我写了代码,一切似乎都很好,我打印了每个和的数字,然后得到数字,但是当我尝试将它们放在td元素中时,表中什么也看不到.我在stackoverflow和google上搜索时,都说了同样的话,但是它不起作用.这是我的代码:

I am writing code for html file that will display the number of each sum of dice for 36000 thrown dices. I wrote code and everything seems fine, I printed the numbers of each sum and I got numbers, but when I try to put them inside the td element, I see nothing in the table. I searched on stackoverflow and on google and all said same thing, but it's not working. Here is my code:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="text/javascript">
            <!--

            /*

                Position #0 : Sum of Dices = 2
                Position #1 : Sum of Dices = 3
                Position #2 : Sum of Dices = 4
                Position #3 : Sum of Dices = 5
                Position #4 : Sum of Dices = 6
                Position #5 : Sum of Dices = 7
                Position #6 : Sum of Dices = 8
                Position #7 : Sum of Dices = 9
                Position #8 : Sum of Dices = 10
                Position #9 : Sum of Dices = 11
                Position #10: Sum of Dices = 12

            */
            var sums = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

            for (var i = 0; i < 36000; i++) {
                var dice1 = Math.floor(Math.random() * 6) + 1;
                var dice2 = Math.floor(Math.random() * 6) + 1;
                var sum = dice1 + dice2;

                switch(sum) {
                    case 2:
                        sums[0]++;
                        break;
                    case 3:
                        sums[1]++;
                        break;
                    case 4:
                        sums[2]++;
                        break;
                    case 5:
                        sums[3]++;
                        break;
                    case 6:
                        sums[4]++;
                        break;
                    case 7:
                        sums[5]++;
                        break;
                    case 8:
                        sums[6]++;
                        break;
                    case 9:
                        sums[7]++;
                        break;
                    case 10:
                        sums[8]++;
                        break;
                    case 11:
                        sums[9]++;
                        break;
                    case 12:
                        sums[10]++;
                        break;
                    default:
                        break;
                }
            }

            document.getElementById("sum2").innerHTML = sums[0];
            document.getElementById("sum3").innerHTML = sums[1];
            document.getElementById("sum4").innerHTML = sums[2];
            document.getElementById("sum5").innerHTML = sums[3];
            document.getElementById("sum6").innerHTML = sums[4];
            document.getElementById("sum7").innerHTML = sums[5];
            document.getElementById("sum8").innerHTML = sums[6];
            document.getElementById("sum9").innerHTML = sums[7];
            document.getElementById("sum10").innerHTML = sums[8];
            document.getElementById("sum11").innerHTML = sums[9];
            document.getElementById("sum12").innerHTML = sums[10];

            // -->
        </script>
    </head>

    <body>

        <h1>Roll Dice 36,000 Times</h1>

        <table border="1">
            <th>Sum of Dice</th>
            <th>Total Times Rolled</th>

            <tr>
                <td>2</td>
                <td id="sum2"></td>
            </tr>
            <tr>
                <td>3</td>
                <td id="sum3"></td>
            </tr>
            <tr>
                <td>4</td>
                <td id="sum4"></td>
            </tr>
            <tr>
                <td>5</td>
                <td id="sum5"></td>
            </tr>
            <tr>
                <td>6</td>
                <td id="sum6"></td>
            </tr>
            <tr>
                <td>7</td>
                <td id="sum7"></td>
            </tr>
            <tr>
                <td>8</td>
                <td id="sum8"></td>
            </tr>
            <tr>
                <td>9</td>
                <td id="sum9"></td>
            </tr>
            <tr>
                <td>10</td>
                <td id="sum10"></td>
            </tr>
            <tr>
                <td>11</td>
                <td id="sum11"></td>
            </tr>
            <tr>
                <td>12</td>
                <td id="sum12"></td>
            </tr>
        </table>
    </body>
</html>

有人说使用innerText和textContent,但似乎都没有用.为什么它不起作用?

Some said to use innerText, and textContent but none seem to work. Why is it not working?

我不知道这是否有帮助,但是我在Mac OS X上使用Google Chrome

I don't know if this helps but I'm using Google Chrome on Mac OS X

推荐答案

使用windows.onload函数确保已加载html:

Use windows.onload function to be sure html is loaded:

window.onload=function(){

        document.getElementById("sum2").innerHTML = sums[0];
        document.getElementById("sum3").innerHTML = sums[1];
        document.getElementById("sum4").innerHTML = sums[2];
        document.getElementById("sum5").innerHTML = sums[3];
        document.getElementById("sum6").innerHTML = sums[4];
        document.getElementById("sum7").innerHTML = sums[5];
        document.getElementById("sum8").innerHTML = sums[6];
        document.getElementById("sum9").innerHTML = sums[7];
        document.getElementById("sum10").innerHTML = sums[8];
        document.getElementById("sum11").innerHTML = sums[9];
        document.getElementById("sum12").innerHTML = sums[10];
};

这篇关于使用Javascript更改td元素文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆