首页 > 解决方案 > How to change the color of the navigation bar in Javascript?

问题描述

Like in the stack overflow navigation bar

if i click on questions the color of it should be change instead of orange line..

can anyone help?colo change

标签: javascriptnavigationbar

解决方案


你可以在你的按钮上添加一个监听器,当你按下它时,改变它的颜色。例如,您可以这样做:

var button = document.getElementById('myButton');
button.addEventListener('click', function(e) { 
  button.style.backgroundColor = "red";
});

您还可以更改按钮的 CSS 类,而不是直接使用 javascript 更改其颜色:

var button = document.getElementById('myButton');
button.addEventListener('click', function(e) { 
  button.className = 'selected';
});

CSS:

.selected
{
  background-color: red;
}

推荐阅读