首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果随机num为3- Javascript的倍数,则将表单元格更改为红色。

如果随机num为3- Javascript的倍数,则将表单元格更改为红色。
EN

Stack Overflow用户
提问于 2016-04-02 22:19:45
回答 3查看 213关注 0票数 0

这是我为JS做的第一个项目,我的任务是制作一个从1到100之间随机生成数字的表,并将3倍为红色、2倍为蓝色的单元格的背景色进行更改。我可以用随机数创建表格,但不知道如何更改单元格的背景色。这是我的表代码。

代码语言:javascript
复制
  // creates a string var that holds the table
    var str = "<table border='1'>";
    for (row = 0; row < num; row++) {
        str += "<tr>";
        for (col = 0; col < num; col++) {
            str += "<td>";

            // creates a random number from 1-100
            var randNum = Math.floor(Math.random() * 100) + 1;
            str += randNum;

            // if a random num is a multiple of 3
            if (randNum%3 === 0) {

                // make cell have red background

            }
                    // if a random number is a multiple of 2
            else if (randNum%2 === 0) {

                // make cell have blue background

           }
            str += "</td>";
        }
        str += "</tr>";
    }
    str += "</table>";

我尝试用id=“”设置< td >,然后在if语句中调用它,但它不起作用。

代码语言:javascript
复制
               str += "<td id='redOrBlue'>";
            // creates a random number from 1-100
            var randNum = Math.floor(Math.random() * 100) + 1;
            str += randNum;

            // if a random num is a multiple of 3
            if (randNum%3 === 0) {
                // make cell have red background

              document.getElementById('redOrBlue').style.backgroundColor='red';
            }
                    // if a random number is a multiple of 2
            else if (randNum%2 === 0) {
                // make cell have blue background

                document.getElementById('redOrBlue').style.backgroundColor='blue';
           }
            str += "</td>"; 

任何帮助都将不胜感激。

http://pastebin.com/EueJtT9n -全程序代码

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-02 22:23:38

如果您将td创建放入一个If else语句中,您可以非常简单地这样做:

HTML

代码语言:javascript
复制
<div id="mainDiv">
  <input type="text" id="myNo" />
  <input type="button" id="gridSize" value="Show It" />
</div>

<div id="result"></div>

JS

代码语言:javascript
复制
var button = document.getElementById('gridSize');
button.onclick = function(e) {
  result = document.getElementById('result');
  num = parseInt(document.getElementById('myNo').value);
  alert(num);
  if (isNaN(num)) {
    alert("No Numeric Value!");
  } else {
    result.innerHTML = num;
  }
  var str = "<table border='2'>";
  for (row = 0; row < num; row++) {
    str += "<tr>";
    for (col = 0; col < num; col++) {
      var randNum = Math.floor(Math.random() * 100) + 1;
      if (randNum % 2 === 0) {
        str += '<td style="background: red;">';
      } else if (randNum % 3 === 0) {
        str += '<td style="background: blue;">';
      } else {
        str += "<td>";
    }
    str += randNum + "</td>";

    }
    str += "</tr>";
  }
  str = str + "</table>";
  result.innerHTML = str;
}

jsfiddle 这里

票数 0
EN

Stack Overflow用户

发布于 2016-04-02 22:24:57

for循环中尝试使用以下代码:

代码语言:javascript
复制
            str += "<td"; //Notice how we leave out the second bracket for now

            // creates a random number from 1-100
            var randNum = Math.floor(Math.random() * 100) + 1;

            // if a random num is a multiple of 3
            if (randNum%3 === 0 && randNum%2 === 0) {
                str += ' style="background-color:purple"';
            } else if (randNum%3 === 0) {
                //Add an inline style to change the background colour
                str += ' style="background-color:red"';
            } else if (randNum%2 === 0) {
                //Add an inline style to change the background colour
                str += ' style="background-color:blue"';
            }
            str += ">" + randNum + "</td>"; //Now we add the missing bracket in here

当我们检查条件(随机数)时,这段代码所做的就是让td元素打开,以便向它添加属性。

我尝试用id=“”设置< td >,然后在if语句中调用它,但它不起作用。

当你想一想,这实际上是有道理的,这是不起作用的。id是DOM (Document )元素的一个属性,它本质上是Javascript访问网页的方式。当您尝试使用给定的id获取元素时,它会失败,因为DOM中不存在该元素--目前它只是一个字符串变量。

为了使该方法工作,您必须将“表字符串”添加到DOM中,然后可以访问和更改元素的ID属性。我制作了一个JSFiddle,通过使用id修改方法来模拟您想要的行为。

票数 0
EN

Stack Overflow用户

发布于 2016-04-02 22:37:58

我会坚持你第一次所做的努力。只在开始之前添加一些CSS,类..blue cell..red cell,每个类都有相应的背景颜色。

这将稍微改变您的逻辑,因为您需要:

1)在字符串中添加开头的"td“

2)生成要进入单元格内的数字

3)在字符串中添加“class=”红/蓝-单元格“>”。

4)将数字添加到字符串中

5)关闭"td“标签。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36379385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档