Мне нужно, чтобы последний ребенок не добавлял backgroundColor. Поле сброса должно оставаться белым. Я не хочу давать окну сброса другой класс, чтобы он не менял цвет. Мне нужно, чтобы стили не добавлялись. Любая помощь или толчок в правильном направлении будут очень благодарны. Я пробовал использовать: not (: last-child), но, похоже, это не сработало.
$(".btn").click(function () {
boxColor = $(this).val();
$(".box:not(:last-child)").css('background-color', boxColor);
});
Вот мой код:
$(".btn").click(function () {
boxColor = $(this).val();
$(".box").css('background-color', boxColor);
});
$(".reset").click(function () {
$(".box").removeAttr('style');
});body {
background: lightgrey;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
flex-direction: row;
max-width: 980px;
margin: 0 auto;
}
.col-3 {
flex: 0 0 24.999%;
}
.box {
height: 200px;
max-width: 230px;
margin: 1em;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.box.red {
background: #E53A40;
}
.box.green {
background: #285943;
}
.box.blue {
background: #6AAFE6;
}
.box.white {
background: white;
}
@media (max-width: 768px) {
.col-3 {
flex: 0 0 100%;
}
.box {
max-width: 100%;
margin-bottom: 1em;
}
}<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<meta http-equiv = "X-UA-Compatible" content = "ie=edge">
<title>Test</title>
</head>
<body>
<div class = "container">
<div class = "col-3">
<div class = "box red">
<button class = "btn" value = "#E53A40">Red</button>
</div>
</div>
<div class = "col-3">
<div class = "box green">
<button class = "btn" value = "#285943">Green</button>
</div>
</div>
<div class = "col-3">
<div class = "box blue">
<button class = "btn" value = "#6AAFE6">Blue</button>
</div>
</div>
<div class = "col-3">
<div class = "box white">
<button class = "reset" id = "reset">Reset</button>
</div>
</div>
</div>
</body>
</html>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вместо этого вы должны использовать :last. Селектор :last-child CSS рассматривает элементы внутри тот же контейнер:
The
:last-childCSS pseudo-class represents the last element among a group of sibling elements.ref
$(".btn").click(function () {
boxColor = $(this).val();
$(".box:not(:last)").css('background-color', boxColor);
});
$(".reset").click(function () {
$(".box").removeAttr('style');
});body {
background: lightgrey;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
flex-direction: row;
max-width: 980px;
margin: 0 auto;
}
.col-3 {
flex: 0 0 24.999%;
}
.box {
height: 200px;
max-width: 230px;
margin: 1em;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.box.red {
background: #E53A40;
}
.box.green {
background: #285943;
}
.box.blue {
background: #6AAFE6;
}
.box.white {
background: white;
}
@media (max-width: 768px) {
.col-3 {
flex: 0 0 100%;
}
.box {
max-width: 100%;
margin-bottom: 1em;
}
}<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<meta http-equiv = "X-UA-Compatible" content = "ie=edge">
<title>Test</title>
</head>
<body>
<div class = "container">
<div class = "col-3">
<div class = "box red">
<button class = "btn" value = "#E53A40">Red</button>
</div>
</div>
<div class = "col-3">
<div class = "box green">
<button class = "btn" value = "#285943">Green</button>
</div>
</div>
<div class = "col-3">
<div class = "box blue">
<button class = "btn" value = "#6AAFE6">Blue</button>
</div>
</div>
<div class = "col-3">
<div class = "box white">
<button class = "reset" id = "reset">Reset</button>
</div>
</div>
</div>
</body>
</html>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Вам следует просто использовать селектор элементов :last в своем запросе.
Я думаю, вы также можете просто установить цвет фона на none вместо удаления всего атрибута стиля, на всякий случай, если вы захотите добавить больше стилей css к элементам в будущем, которые не должны быть сброшены.
$(".btn").click(function () {
boxColor = $(this).val();
$(".box:not(:last)").css('background-color', boxColor);
});
$(".reset").click(function () {
$(".box").css('background-color', 'none');
});
Вопрос не в том, как не выбирать
:last-child, потому что здесь речь идет не о родственных элементах и не о селекторе CSS, а о селекторе jQuery.