单击时如何更改按钮颜色?

时间:2021-04-22 10:24:48

标签: javascript html css layout

有一个这样的网站:

Layout the header of the site

有必要当你点击一个按钮时,这个按钮的颜色变成淡紫色,而之前选择的按钮变成白色。

    $(document).ready(function() {
        all__buttons=document.querySelectorAll('.header__button');
        /*selected__button.click= function(){
                selected__button.backgroundColor='#5f3ec0';
            
        };*/
    
        function changeColor(inputbutton) {
            inputbutton.style.backgroundColor='#5f3ec0';
        
        }
    
            [].forEach.call(all__buttons, function(selected__button){
                selected__button.addEventListener('click', changeColor);
            }); 
    }
    )
body {
    margin: 0;
    padding: 0;
}

.globalcontainer {
    margin-top: 0px;
    margin-right: 15px;
    margin-bottom: 0px;
    margin-left: 15px;
}

.header > .header__text {
position: relative;
width: 93px;
height: 24px;
left: 0;
margin-top: 129px;
margin-bottom: 20px;

font-family: TT Norms;
font-style: normal;
font-weight: bold;
font-size: 24px;
line-height: 100%;
/* identical to box height, or 24px */


color: #000000;
}

/*
.header__buttons {
    left: 0;
    top: 193px;
}
*/


.header__button {
    border-radius: 3px;
    background-color: #fff;
}
<html>
<head>
<!-- <link rel="stylesheet" href="testsite.css">-->
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Example</title>
<link rel="stylesheet" type="text/css" href="../css/mobile.css" media="screen and (min-width: 320px) and (max-width: 639px)"></link>
<link rel="stylesheet" type="text/css" href="../css/tablet.css" media="screen and (min-width: 640px) and (max-width: 1023px)"></link>
<link rel="stylesheet" type="text/css" href="../css/desktop.css" media="screen and (min-width: 1024px) and (max-width: 1920px)"></link>
<script type="text/javascript" src="http://code.jquery.com/jquery-3.6.0.min.js"></script>

</head>
<body>
    <div class="globalcontainer">
        <div class="header">
                <div class="header__text">
                    Обзоры
                </div>
                <div class="header__buttons">
                    <span>
                        <button class="header__button">Все</button>
                        <button class="header__button">Видео</button>               
                        <button class="header__button">Текст</button>               
                        <button class="header__button">Обзоры</button>
                        <button class="header__button">Сравнения</button>
                        <button class="header__button">Краш видео</button>
                        <button class="header__button">Распаковка</button>
                    </span>
                </div>
        </div>      
        <div class="content">       
                <div class="cardexample">
                </div>
                <div class="cardexample">
                </div>
                <div class="cardexample">
                </div>
                <div class="cardexample">
                </div>
        </div>      
    </div>
<script type="text/javascript" src="jsactions.js"></script>
</body>
</html>

但结果,点击鼠标后,按钮颜色并没有变成淡紫色。告诉我如何在点击时更改按钮的颜色。

1 个答案:

答案 0 :(得分:1)

$(document).ready(function() {
   $('.header__button').click(function(){
       $('.header__button').removeClass('lilac');
       $(this).addClass('lilac');
   });            
});
body {
    margin: 0;
    padding: 0;
}

.globalcontainer {
    margin-top: 0px;
    margin-right: 15px;
    margin-bottom: 0px;
    margin-left: 15px;
}

.header > .header__text {
position: relative;
width: 93px;
height: 24px;
left: 0;
margin-top: 129px;
margin-bottom: 20px;

font-family: TT Norms;
font-style: normal;
font-weight: bold;
font-size: 24px;
line-height: 100%;
/* identical to box height, or 24px */


color: #000000;
}

/*
.header__buttons {
    left: 0;
    top: 193px;
}
*/


.header__button {
    border-radius: 3px;
    background-color: #fff;
}
.lilac {
    background-color: #5f3ec0;
}
<script type="text/javascript" src="http://code.jquery.com/jquery-3.6.0.min.js"></script>

<div class="globalcontainer">
        <div class="header">
                <div class="header__text">
                    Обзоры
                </div>
                <div class="header__buttons">
                    <span>
                        <button class="header__button">Все</button>
                        <button class="header__button">Видео</button>               
                        <button class="header__button">Текст</button>               
                        <button class="header__button">Обзоры</button>
                        <button class="header__button">Сравнения</button>
                        <button class="header__button">Краш видео</button>
                        <button class="header__button">Распаковка</button>
                    </span>
                </div>
        </div>      
        <div class="content">       
                <div class="cardexample">
                </div>
                <div class="cardexample">
                </div>
                <div class="cardexample">
                </div>
                <div class="cardexample">
                </div>
        </div>      
</div>