Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

javascript - 如何使用两个按钮的onClick()事件在JavaScript中更改两种不同的背景颜色(How to change two different background colors in JavaScript using onClick() event for two buttons)

I have to make two buttons that will change the background color onclick, the first button will change the background color to yellow and the other one will change the background color into light blue.(我必须制作两个按钮来更改onclick的背景色,第一个按钮将背景色更改为黄色,另一个按钮将背景色更改为浅蓝色。)

I have done something like this,(我做了这样的事情,) <button id="bttn1" type="button" onclick="changeColor()">Click for a yellow color</button> <button id="bttn2" type="button" onclick="changeColor()">Click for a light blue color</button> function changeColor() { document.getElementById("bttn1").style.backgroundColor = "yellow"; document.getElementById("bttn2").style.backgroundColor = "light blue"; }   ask by Tyaa Jaynar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using event.target(使用event.target)

function changeColor(color) { event.target.style.backgroundColor = color; } <button id="bttn1" type="button" onclick="changeColor('yellow')">Click for a yellow color</button> <button id="bttn2" type="button" onclick="changeColor('lightblue')">Click for a light blue color</button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...