Button color change when selected

坚强是说给别人听的谎言 提交于 2019-12-12 03:09:10

问题


I have 10 buttons in one HTML page. I want to use JavaScript to change particular button (button 1) color when button is selected. When user click another button (button 2) the color of that button is should change and first selected button (button 1) should be deselected.


回答1:


You can define a different CSS class for selected button.

.button-selected {
    border: 2px solid red;
}

Then when a button is clicked, you can call the function like this:

function focusMe(button) {
    document.getElementsByClassName("button-selected")[0].className = "";
    button.className = "button-selected";
}

Add this to HTML

<button name="button1" onClick="focusMe(this);" />


来源:https://stackoverflow.com/questions/14385572/button-color-change-when-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!