[英]How to change the color of the navigation bar in Javascript?
You can add a listener on your button, and when you press it, change the color of it.你可以在你的按钮上添加一个监听器,当你按下它时,改变它的颜色。 You can do this for example :
例如,您可以这样做:
var button = document.getElementById('myButton');
button.addEventListener('click', function(e) {
button.style.backgroundColor = "red";
});
You can also change the CSS class of your button instead of changing its color directly with javascript :您还可以更改按钮的 CSS 类,而不是直接使用 javascript 更改其颜色:
var button = document.getElementById('myButton');
button.addEventListener('click', function(e) {
button.className = 'selected';
});
CSS : CSS :
.selected
{
background-color: red;
}
你可以用这样的 javascript 来做到这一点:
document.getElementById("elementId").style.backgroundColor = "blue";
Try This answer试试这个答案
function getElementById_Click() { document.getElementById("example").style.backgroundColor = "red"; } function getElementsByClassName_Click() { document.getElementsByClassName("example")[2].style.backgroundColor = "red"; }
<!DOCTYPE html> <html> <head> <style> .example, #example { border: 1px solid black; margin: 5px; } </style> </head> <body> <div class="example "> This is getElementsByClassName Example 1 </div> <div id="example"> This is getElementById Example </div> <div class="example"> This is getElementsByClassName Example 2 </div> <div class="example"> This is getElementsByClassName Example 3 </div> <div class="example"> This is getElementsByClassName Example 4 </div> <button type="button" onclick="getElementById_Click()" >GET ELEMENT BY ID</button> <button type="button" onclick="getElementsByClassName_Click()" >GET ELEMENT BY CLASS</button> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.