На этой странице куча кнопок (элемент привязки) имеет свой номер (id), я должен выбрать их и поместить в массив, когда они переключаются как активные. Я уже создал массив, добавляются идентификаторы кнопок, и все они печатаются, но я не могу использовать этот класс «.active» вместо моего обычного класса «btn_reservas».
Как я могу добавить/удалить в свой массив только выбранные числа?
var controller = (function () {
var ctrlAddItem = function (event) {
console.info("It worked, pressed id = " + event.target.id);
event.target.classList.toggle("active");
};
document.querySelectorAll(".btn_reservas").forEach(function () {
this.addEventListener("click", ctrlAddItem);
});
document.addEventListener("keypress", function (event) {
if (event.keyCode === 13 || event.which === 13) {
ctrlAddItem();
}
});
var sum = 0;
var x = document.getElementsByClassName("btn_reservas");
// var x = document.getElementsByClassName("btn_reservas.active");
var numbers = [];
for (i=0; i < x.length; i++){
x[i].id;
numbers.push(', ' + x[i].id);
}
numbers.forEach(myFunction);
function myFunction(item) {
sum += item;
document.getElementById("demo").innerHTML = sum;
}
})();
body {
background-color: #ffffff;
}
h1 {
color: #999999;
align-content: center;
padding-left: 400px;
}
#menu {
background-color: #4682b4;
padding: 3px;
}
#menu ul {
text-align: center;
padding-left: 20%;
list-style: none;
}
#menu ul li {
display: inline;
}
#menu ul li a {
color: #ffffff;
/* padding-left: 5%; */
margin: 50px;
background-color: #4682b4;
font-size: xx-large;
text-decoration: none;
}
#menu ul li a:hover {
background-color: #b0c4de;
}
#Promocoes {
display: flex;
flex-direction: row;
}
#Promocoes img {
align-content: center;
padding: 25px;
}
#item {
text-align: center;
border-radius: 10px;
margin: 20px;
background-color: rgba(9, 50, 99, 0.35);
}
#item p {
font-size: 14px;
}
#item button {
background-color: #ffffff;
padding: 10px;
border-radius: 5px;
cursor: pointer;
border: none;
font-weight: bold;
}
#item button:hover {
background-color: #b0c4de;
}
/* Sobre Nos */
#historia {
margin-left: 200px;
margin-right: 200px;
font-size: 20px;
}
#historia img {
padding-left: 100px;
align-self: center;
}
/* SORTEIO */
.legenda ul li{
list-style: none;
display: inline-block;
font-weight: bolder;
margin-right: 1%;
margin-top: 5px;
margin-bottom: 10px;
}
.disponivel {
cursor: default;
background-color: #ffffff;
border: 2px #a9a9a9 solid;
border-radius: 5px;
padding: 8px;
}
.reservados {
cursor: not-allowed;
background-color: lightgreen;
border: 2px green solid;
color: #ffffff;
border-radius: 5px;
padding: 8px;
}
.pagos {
cursor: not-allowed;
background-color: lightsalmon;
border: 2px indianred solid;
color: #ffffff;
border-radius: 5px;
padding: 8px;
}
.enviar {
background-color: #4682b4;
cursor: pointer;
color: #ffffff;
border: #4682b4 solid;
border-radius: 5px;
padding: 8px;
}
.lista_sorteio {
margin-left: 200px;
margin-right: 200px;
font-size: 20px;
}
.lista ul li {
display: inline;
}
.lista ul li a {
display: block;
border: 2px solid #bfc0bf;
border-radius: 50%;
width: 100%;
line-height: 40px;
max-width: 75px;
height: auto;
font-weight: 700;
text-decoration: none;
display: inline;
box-sizing: border-box;
padding: 20px;
font-family: sans-serif;
font-size: 13px;
color: #ffffff;
background-color:rgb(85, 161, 108);
border-color: #212529;
margin-right: 50px;
}
.btn {
display: inline-block;
font-weight: 400;
color: #212529;
text-align: center;
vertical-align: middle;
cursor: pointer;
-webkit-user-select: none;
user-select: none;
background-color: transparent;
border: 1px solid transparent;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: .25rem;
transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
.lista ul li a:hover {
color: #212529;
background-color: #ffffff;
font:bolder;
transition: all 600ms ease;
}
.lista ul li a.active {
background-color: #f90;
}
<div class = "lista">
<ul >
<li>
<a href = "#000" id = "000" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">001</a>
</li>
<li>
<a href = "#001" id = "001" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">002</a>
</li>
<li>
<a href = "#002" id = "002" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">003</a>
</li>
<li>
<a href = "#003" id = "003" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">004</a>
</li>
<li>
<a href = "#004" id = "004" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">005</a>
</li>
<li>
<a href = "#005" id = "005" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">006</a>
</li>
<li>
<a href = "#006" id = "006" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">007</a>
</li>
<li>
<a href = "#007" id = "007" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">008</a>
</li>
<li>
<a href = "#008" id = "008" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">009</a>
</li>
</ul>
</div>
</div>
<hr>
<p>Get the sum of the numbers in the array.</p>
<p id = "demo"></p>
<hr>
</body>
</html>
Вы все еще можете ссылаться на .btn_reservas.active
, вам просто нужно использовать querySelectorAll
вместо getElementsByClassName
.
Это по-прежнему будет вычислять сумму, но не беспокоится о массиве, поскольку он просто ищет элементы с .btn_reservas.active
.
var controller = (function() {
function calcSum(){
toggled = document.querySelectorAll(".btn_reservas.active");
sel =[];
toggled.forEach(function(el){
sel.push(el.getAttribute("id"));
});
document.getElementById("demo").innerHTML = sel.join(", ");
}
var ctrlAddItem = function(event) {
console.info("It worked, pressed id = " + event.target.id);
event.target.classList.toggle("active");
calcSum();
};
document.querySelectorAll(".btn_reservas").forEach(function() {
this.addEventListener("click", ctrlAddItem);
});
document.addEventListener("keypress", function(event) {
if (event.keyCode === 13 || event.which === 13) {
ctrlAddItem();
}
});
})();
body {
background-color: #ffffff;
}
h1 {
color: #999999;
align-content: center;
padding-left: 400px;
}
#menu {
background-color: #4682b4;
padding: 3px;
}
#menu ul {
text-align: center;
padding-left: 20%;
list-style: none;
}
#menu ul li {
display: inline;
}
#menu ul li a {
color: #ffffff;
/* padding-left: 5%; */
margin: 50px;
background-color: #4682b4;
font-size: xx-large;
text-decoration: none;
}
#menu ul li a:hover {
background-color: #b0c4de;
}
#Promocoes {
display: flex;
flex-direction: row;
}
#Promocoes img {
align-content: center;
padding: 25px;
}
#item {
text-align: center;
border-radius: 10px;
margin: 20px;
background-color: rgba(9, 50, 99, 0.35);
}
#item p {
font-size: 14px;
}
#item button {
background-color: #ffffff;
padding: 10px;
border-radius: 5px;
cursor: pointer;
border: none;
font-weight: bold;
}
#item button:hover {
background-color: #b0c4de;
}
/* Sobre Nos */
#historia {
margin-left: 200px;
margin-right: 200px;
font-size: 20px;
}
#historia img {
padding-left: 100px;
align-self: center;
}
/* SORTEIO */
.legenda ul li {
list-style: none;
display: inline-block;
font-weight: bolder;
margin-right: 1%;
margin-top: 5px;
margin-bottom: 10px;
}
.disponivel {
cursor: default;
background-color: #ffffff;
border: 2px #a9a9a9 solid;
border-radius: 5px;
padding: 8px;
}
.reservados {
cursor: not-allowed;
background-color: lightgreen;
border: 2px green solid;
color: #ffffff;
border-radius: 5px;
padding: 8px;
}
.pagos {
cursor: not-allowed;
background-color: lightsalmon;
border: 2px indianred solid;
color: #ffffff;
border-radius: 5px;
padding: 8px;
}
.enviar {
background-color: #4682b4;
cursor: pointer;
color: #ffffff;
border: #4682b4 solid;
border-radius: 5px;
padding: 8px;
}
.lista_sorteio {
margin-left: 200px;
margin-right: 200px;
font-size: 20px;
}
.lista ul li {
display: inline;
}
.lista ul li a {
display: block;
border: 2px solid #bfc0bf;
border-radius: 50%;
width: 100%;
line-height: 40px;
max-width: 75px;
height: auto;
font-weight: 700;
text-decoration: none;
display: inline;
box-sizing: border-box;
padding: 20px;
font-family: sans-serif;
font-size: 13px;
color: #ffffff;
background-color: rgb(85, 161, 108);
border-color: #212529;
margin-right: 50px;
}
.btn {
display: inline-block;
font-weight: 400;
color: #212529;
text-align: center;
vertical-align: middle;
cursor: pointer;
-webkit-user-select: none;
user-select: none;
background-color: transparent;
border: 1px solid transparent;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: .25rem;
transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
}
.lista ul li a:hover {
color: #212529;
background-color: #ffffff;
font: bolder;
transition: all 600ms ease;
}
.lista ul li a.active {
background-color: #f90;
}
<div class = "lista">
<ul>
<li>
<a href = "#000" id = "000" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">001</a>
</li>
<li>
<a href = "#001" id = "001" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">002</a>
</li>
<li>
<a href = "#002" id = "002" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">003</a>
</li>
<li>
<a href = "#003" id = "003" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">004</a>
</li>
<li>
<a href = "#004" id = "004" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">005</a>
</li>
<li>
<a href = "#005" id = "005" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">006</a>
</li>
<li>
<a href = "#006" id = "006" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">007</a>
</li>
<li>
<a href = "#007" id = "007" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">008</a>
</li>
<li>
<a href = "#008" id = "008" class = "btn_reservas" data-original-title = "Número 001 Pago por: Pedro">009</a>
</li>
</ul>
</div>
</div>
<hr>
<p>Get the sum of the numbers in the array.</p>
<p id = "demo"></p>
<hr>
</body>
</html>
@Akilleks, я обновил свой ответ. Он использует массив, просто помещает идентификаторы в массив, а затем объединяет их на выходе для отображения с запятой.
Это блестяще! Огромное спасибо. Я так спешил и не мог понять, как это сделать, это решение выше совершенства.
Это восхитительно! Большое спасибо! Но на самом деле моя функция суммирования неверна, я хочу отображать каждое число рядом, разделенное запятой ",". Не могли бы вы изменить это?