简体   繁体   English

按钮不会改变颜色

[英]button won't change color

I have made 30 buttons in JavaScript now I want all buttons to be green like the first button and whenever you click on a button it turns and stays red.我在 JavaScript 中制作了 30 个按钮,现在我希望所有按钮都像第一个按钮一样为绿色,并且每当您单击一个按钮时,它都会变为红色并保持红色。

For some reason only the first button is green and whenever I click it it won't turn red.出于某种原因,只有第一个按钮是绿色的,每当我点击它时它都不会变成红色。 I tried using: button.style.backgroundColor = "red";我尝试使用: button.style.backgroundColor = "red"; in a function to make the button red when clicked but this doesn't work在 function 中使按钮在单击时变为红色,但这不起作用

 const color = ["green"]; page(); function onbuttonclicked() { document.body.style.backgroundColor = color[nr - 1]; } function page() { document.body.style.backgroundColor = "grey"; //style page createButtons(30); } function set_onclick(amount) { for (var a = 1; a < (amount + 1); a++) { document.getElementById("button" + a).setAttribute("onclick", "onbuttonclicked(" + a + ")"); } } function createButtons(amount) { for (var a = 1; a < (amount + 1); a++) { var button = document.createElement("button"); button.style.backgroundColor = color[a - 1]; button.id = "button" + a; button.innerHTML = "button " + a; container.appendChild(button); } set_onclick(amount); }
 <div id="container"></div>

EDIT thanks for all the answers it isnt possible to only change certain buttons when you click on them right?编辑感谢所有答案,当您单击它们时,不可能只更改某些按钮吗? so when i click on button1 nothing happens but whenever i click on button 3 it turn red所以当我点击按钮 1 时没有任何反应,但每当我点击按钮 3 时它变成红色

You need to pass nr as a parameter in onbuttonclicked function您需要在onbuttonclicked function 中将nr作为参数传递

EDIT:编辑:

OP Comment:操作评论:

okay so i need to add an parameter.好的,所以我需要添加一个参数。 but i want all 30 buttons to be green so when you open the page all buttons are green this also has to do with the parameter?但是我希望所有 30 个按钮都是绿色的,所以当您打开页面时,所有按钮都是绿色的,这也与参数有关吗?

For that in your loop createButton, change color[a - 1] for color[0]为此,在您的循环 createButton 中,将color[a - 1]更改为color[0]

 const color = ["green"]; function onbuttonclicked(nr) { document.body.style.backgroundColor = color[nr - 1]; } function page() { //style page document.body.style.backgroundColor = "grey"; createButtons(30); } function set_onclick(amount, nr) { for (let a = 1; a < (amount + 1); a++) { document.getElementById(`button${a}`).setAttribute("onclick", `onbuttonclicked(${nr})`); } } function createButtons(amount) { for (let a = 1; a < (amount + 1); a++) { const button = document.createElement("button"); button.style.backgroundColor = color[0]; button.id = `button${a}`; button.innerHTML = `button ${a}`; container.appendChild(button); } set_onclick(amount, 1); } page();
 <div id="container"></div>

A common way to do this is to use CSS classes :一种常见的方法是使用CSS 类

 page(); function onbuttonclicked(a) { //document.body.style.backgroundColor = color[nr - 1]; document.getElementById("button" + a).classList.remove("button") document.getElementById("button" + a).classList.add("clickedbutton") } function set_onclick(amount) { for (var a = 1; a < (amount + 1); a++) { document.getElementById("button" + a).setAttribute("onclick", "onbuttonclicked(" + a + ")"); } } function page() { document.body.style.backgroundColor = "grey"; //style page createButtons(30); } function createButtons(amount){ for (var a = 1;a <(amount + 1); a++) { var button = document.createElement("button"); button.classList.add("button") button.id = "button" + a; button.innerHTML = "button " + a; container.appendChild(button); } set_onclick(amount); }
 .clickedbutton { background-color: red; }.button { background-color: green; }
 <div id="container"></div>

 const number_of_buttons = 30; createButtons(); function createButtons(){ var container = document.getElementById("container"); for(var i=1;i<=number_of_buttons;i++){ var button = document.createElement("button"); button.innerText = "Button " + i; button.style.backgroundColor = "green"; button.onclick = function(event){ var source = event.target || event.srcElementl source.style.backgroundColor = "red"; }; container.appendChild(button); } }
 button{ margin: 10px; font-family: 'Consolas'; font-size: 20px; padding: 5px; outline: none; cursor: pointer; transition: 0.5s; } button:hover{ border: 1px solid black; } button:active{ transform: translateY(2px); }
 <div id="container"> </div>

This is what you have asked for!这就是你所要求的!

I did the styling for better looks!为了更好看,我做了造型!

  1. This line: button.style.backgroundColor = color[a - 1];这一行: button.style.backgroundColor = color[a - 1]; is wrong, since your color array only contains a single string ( "green" ).是错误的,因为您的color数组只包含一个字符串( "green" )。 You should do color[0] , or, you need to populate the array to have 30 elements.您应该执行color[0] ,或者,您需要将数组填充为具有 30 个元素。
  2. document.body.style.backgroundColor = color[nr - 1]; - Where is this nr variable came from? - 这个nr变量是从哪里来的? The correct line should be button.style.backgroundColor = "red" ;正确的行应该是button.style.backgroundColor = "red" ; or - again - you can populate the color (ie const color=["green", "red"] ) and use color[1] .或者 - 再次 - 您可以填充color (即const color=["green", "red"] )并使用color[1]

Check out this demo看看这个演示

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

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