我在页面上有四个框,当用户单击它们时要更改颜色。我给每个盒子一个ID:

<div id="boxa" onclick="changeColorA()"></div>
<div id="boxb" onclick="changeColorB()"></div>
<div id="boxc" onclick="changeColorC()"></div>
<div id="boxd" onclick="changeColorD()"></div>


如您所见,为了使用onclick更改框的颜色,我不得不为每个框编写一个单独的函数:

function changeColorA() {
  var col = '#'+Math.floor(Math.random()*16777215).toString(16);
  document.getElementById("boxa").style.backgroundColor=col;
}

function changeColorB() {
  var col = '#'+Math.floor(Math.random()*16777215).toString(16);
  document.getElementById("boxb").style.backgroundColor=col;
}

function changeColorC() {
  var col = '#'+Math.floor(Math.random()*16777215).toString(16);
  document.getElementById("boxc").style.backgroundColor=col;
}

function changeColorD() {
  var col = '#'+Math.floor(Math.random()*16777215).toString(16);
  document.getElementById("boxd").style.backgroundColor=col;
}


我已经在iPad上的浏览器中运行了该程序,但运行速度太慢。我该如何重构,以便只有一个功能可以针对用户单击的任何框?



function changeColorA() {
  var col = '#' + Math.floor(Math.random() * 16777215).toString(16);
  document.getElementById("boxa").style.backgroundColor = col;
}

function changeColorB() {
  var col = '#' + Math.floor(Math.random() * 16777215).toString(16);
  document.getElementById("boxb").style.backgroundColor = col;
}

function changeColorC() {
  var col = '#' + Math.floor(Math.random() * 16777215).toString(16);
  document.getElementById("boxc").style.backgroundColor = col;
}

function changeColorD() {
  var col = '#' + Math.floor(Math.random() * 16777215).toString(16);
  document.getElementById("boxd").style.backgroundColor = col;
}

body {
  margin: 10px;
  width: 758px;
  height: 1014px;
}

#main {
  background-color: #00b894;
  width: 748px;
  height: 1004px;
}

.container-1 {
  text-align: center;
}

.container-2 {
  text-align: center;
}

#boxa {
  margin-top: 250px;
  margin-right: 20px;
  width: 200px;
  height: 200px;
  background-color: #fab1a0;
  border-radius: 20px;
  display: inline-block;
}

#boxb {
  width: 200px;
  height: 200px;
  background-color: #00cec9;
  border-radius: 20px;
  display: inline-block;
}

#boxc {
  margin-top: 20px;
  margin-right: 20px;
  width: 200px;
  height: 200px;
  background-color: #55efc4;
  border-radius: 20px;
  display: inline-block;
}

#boxd {
  width: 200px;
  height: 200px;
  background-color: #6c5ce7;
  border-radius: 20px;
  display: inline-block;
}

<div id="boxa" onclick="changeColorA()"></div>
<div id="boxb" onclick="changeColorB()"></div>
<div id="boxc" onclick="changeColorC()"></div>
<div id="boxd" onclick="changeColorD()"></div>





Codepen:https://codepen.io/simonrevill/pen/WzZReX

最佳答案

您可以尝试以下方法:



function changeColor(element) {
  var col = '#' + Math.floor(Math.random() * 16777215).toString(16);
  element.style.backgroundColor = col;
}

#boxa {
  width: 100px;
  height: 100px;
  background-color: #fab1a0;
  border-radius: 10px;
  display: inline-block;
}

#boxb {
  width: 100px;
  height: 100px;
  background-color: #00cec9;
  border-radius: 10px;
  display: inline-block;
}

#boxc {
  width: 100px;
  height: 100px;
  background-color: #55efc4;
  border-radius: 10px;
  display: inline-block;
}

#boxd {
  width: 100px;
  height: 100px;
  background-color: #6c5ce7;
  border-radius: 10px;
  display: inline-block;
}

<div id="boxa" onclick="changeColor(this)"></div>
<div id="boxb" onclick="changeColor(this)"></div>
<div id="boxc" onclick="changeColor(this)"></div>
<div id="boxd" onclick="changeColor(this)"></div>

关于javascript - 重构JS函数以定位多个ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49490303/

10-11 05:44