简体   繁体   English

将数组中的项目与JavaScript,初学者中的表项目进行比较

[英]Compare items in an array against table items in JavaScript, Beginner

I created a really basic table with 20 numbers inside. 我创建了一个真正的基本表,里面有20个数字。 I have then bodged together a system which creates an array of 20 numbers and shuffles it, there is then a button which displays one position of the array at a time. 然后,我将一个系统组合在一起,该系统创建一个由20个数字组成的数组并对其进行混洗,然后有一个按钮可一次显示该数组的一个位置。

What I want to do, which I haven't been able to figure out yet, is to use the number that has been generated to change to background color of that same number in the table. 我现在还无法弄清楚的是,要使用生成的数字将表中的相同数字更改为背景色。 So if I drew the number 20 it would be marked on the table. 因此,如果我画了数字20,它将在桌子上标记出来。

Please could you respond with JavaScript only as jQuery is still a little unknown at the moment. 请问您是否只能使用JavaScript进行响应,因为jQuery目前还是一个未知数。

 // This bit shuffles an array function shuffle(array) { var i = array.length, j = 0, temp; while (i--) { j = Math.floor(Math.random() * (i+1)); // swap randomly chosen element with current element temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } // Array input var ranNums = shuffle([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]); //This bit calls the position of the array var f = 0; // the index of the current item to show function nextNumber() { document .getElementById('test') .innerHTML = ranNums[f++]; // get the item and increment if (f == ranNums.length) f = 0; // reset to first element if you've reached the end } 
 body { background-color: white; color: black; font-size: 20px; font-family: "Lucida Grande", Verdana,Arial, Helvetica, sans-serif; } h1, th { font-family: Georgia, "Times New Roman",Times, serif; } h1 { font-size: 28px; } table { border-collapse: separate; border-spacing: 30px; float: left; } th, td { padding: 30px; border: 2px black solid; text-align: center; width: 20%; } h2 { } button { } #item20 { background-color: red; } 
 <h1>Bingo!</h1> <h2 id="test"></h2> <table> <tr> <td id="item1"<h1>1</h1></td> <td id="item2"<h1>2</h1></td> <td id="item3"<h1>3</h1></td> <td id="item4"<h1>4</h1></td> <td id="item5"<h1>5</h1></td> </tr> <tr> <td id="item6"<h1>6</h1></td> <td id="item7"<h1>7</h1></td> <td id="item8"<h1>8</h1></td> <td id="item9"<h1>9</h1></td> <td id="item10"<h1>10</h1></td> </tr> <tr> <td id="item11"<h1>11</h1></td> <td id="item12"<h1>12</h1></td> <td id="item13"<h1>13</h1></td> <td id="item14"<h1>14</h1></td> <td id="item15"<h1>15</h1></td> </tr> <tr> <td id="item16"<h1>16</h1></td> <td id="item17"<h1>17</h1></td> <td id="item18"<h1>18</h1></td> <td id="item19"<h1>19</h1></td> <td id="item20"<h1>20</h1></td> </tr> </table> <button onclick="nextNumber()">Next Number</button> 

Change your nextNumber method to also highlight the new random number selected 更改您的nextNumber方法以突出显示所选的新随机数

function nextNumber() {
  var number = ranNums[f];
  f++;
  document.getElementById('test').innerHTML = number;
  document.getElementById("item" + number).style.backgroundColor = "red";
  if (f == ranNums.length) f = 0; // reset to first element if you've reached the end    
}

Demo 演示版

 // This bit shuffles an array function shuffle(array) { var i = array.length, j = 0, temp; while (i--) { j = Math.floor(Math.random() * (i + 1)); // swap randomly chosen element with current element temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } // Array input var ranNums = shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]); //This bit calls the position of the array var f = 0; // the index of the current item to show function nextNumber() { var number = ranNums[f]; f++; document.getElementById('test').innerHTML = number; document.getElementById("item" + number).style.backgroundColor = "red"; if (f == ranNums.length) f = 0; // reset to first element if you've reached the end } 
 body { background-color: white; color: black; font-size: 20px; font-family: "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; } h1, th { font-family: Georgia, "Times New Roman", Times, serif; } h1 { font-size: 28px; } table { border-collapse: separate; border-spacing: 30px; float: left; } th, td { padding: 30px; border: 2px black solid; text-align: center; width: 20%; } h2 {} button {} #item20 { background-color: red; } 
 <h1>Bingo!</h1> <h2 id="test"></h2> <table> <tr> <td id="item1" <h1>1</h1> </td> <td id="item2" <h1>2</h1> </td> <td id="item3" <h1>3</h1> </td> <td id="item4" <h1>4</h1> </td> <td id="item5" <h1>5</h1> </td> </tr> <tr> <td id="item6" <h1>6</h1> </td> <td id="item7" <h1>7</h1> </td> <td id="item8" <h1>8</h1> </td> <td id="item9" <h1>9</h1> </td> <td id="item10" <h1>10</h1> </td> </tr> <tr> <td id="item11" <h1>11</h1> </td> <td id="item12" <h1>12</h1> </td> <td id="item13" <h1>13</h1> </td> <td id="item14" <h1>14</h1> </td> <td id="item15" <h1>15</h1> </td> </tr> <tr> <td id="item16" <h1>16</h1> </td> <td id="item17" <h1>17</h1> </td> <td id="item18" <h1>18</h1> </td> <td id="item19" <h1>19</h1> </td> <td id="item20" <h1>20</h1> </td> </tr> </table> <button onclick="nextNumber()">Next Number</button> 

You could clean this up a bit, but this one uses a red class. 您可以稍微清理一下,但是这个使用red类。 The key is to target the box using document.getElementById("item" + randNum).className = "red"; 关键是使用document.getElementById("item" + randNum).className = "red";来定位盒子document.getElementById("item" + randNum).className = "red";

I have added a resetNumbers() function and button to reset your classes. 我添加了resetNumbers()函数和按钮来重置您的类。 This is to display the reason why I chose to use a class. 这是为了显示我选择使用类的原因。 Originally I had it resetting after every button click, but if this is BINGO, that probably is not desired. 最初,每次单击按钮后我都会将其重置,但是如果这是BINGO,则可能不希望这样做。 I commented out where I was doing this, but you can still reset. 我注释掉了我在哪里执行此操作,但是您仍然可以重置。

I prefer this to setting the background colour, as this is controlled through a css class instead, which is easier to manage. 我更喜欢此设置背景色,因为它是通过css类控制的,因此更易于管理。

 // This bit shuffles an array function shuffle(array) { var i = array.length, j = 0, temp; while (i--) { j = Math.floor(Math.random() * (i+1)); // swap randomly chosen element with current element temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } // Array input var ranNums = shuffle([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]); //This bit calls the position of the array var f = 0; // the index of the current item to show function nextNumber() { var randNum = ranNums[f++]; document.getElementById('test').innerHTML = randNum; // get the item and increment if (f == ranNums.length) f = 0; // reset to first element if you've reached the end //resetNumbers();//uncomment if you do not want to reset - or remove if you never want this here document.getElementById("item" + randNum).className = "red"; } function resetNumbers() { for (var i = 0; i < ranNums.length; i++) { document.getElementById("item" + ranNums[i]).className = ""; } } 
 body { background-color: white; color: black; font-size: 20px; font-family: "Lucida Grande", Verdana,Arial, Helvetica, sans-serif; } h1, th { font-family: Georgia, "Times New Roman",Times, serif; } h1 { font-size: 28px; } table { border-collapse: separate; border-spacing: 30px; float: left; } th, td { padding: 30px; border: 2px black solid; text-align: center; width: 20%; } .red { background-color: red; } 
 <h1>Bingo!</h1> <h2 id="test"></h2> <table> <tr> <td id="item1"<h1>1</h1></td> <td id="item2"<h1>2</h1></td> <td id="item3"<h1>3</h1></td> <td id="item4"<h1>4</h1></td> <td id="item5"<h1>5</h1></td> </tr> <tr> <td id="item6"<h1>6</h1></td> <td id="item7"<h1>7</h1></td> <td id="item8"<h1>8</h1></td> <td id="item9"<h1>9</h1></td> <td id="item10"<h1>10</h1></td> </tr> <tr> <td id="item11"<h1>11</h1></td> <td id="item12"<h1>12</h1></td> <td id="item13"<h1>13</h1></td> <td id="item14"<h1>14</h1></td> <td id="item15"<h1>15</h1></td> </tr> <tr> <td id="item16"<h1>16</h1></td> <td id="item17"<h1>17</h1></td> <td id="item18"<h1>18</h1></td> <td id="item19"<h1>19</h1></td> <td id="item20"<h1>20</h1></td> </tr> </table> <button onclick="nextNumber()">Next Number</button> <button onclick="resetNumbers()">Reset Numbers</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM