Compare commits
1 Commits
solucion-e
...
solucion-e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3016e835c4 |
@@ -1,11 +1 @@
|
||||
const lista = document.querySelector("#lista");
|
||||
|
||||
// Un solo listener en la lista, no uno por ítem (delegación de eventos).
|
||||
lista.addEventListener("click", (e) => {
|
||||
// Verificamos que el clic fue sobre un <li>. Si fue sobre otro elemento,
|
||||
// retornamos sin hacer nada.
|
||||
if (e.target.tagName !== "LI") return;
|
||||
|
||||
// toggle agrega la clase si no la tiene, y la quita si ya la tenía.
|
||||
e.target.classList.toggle("seleccionado");
|
||||
});
|
||||
// Agregar aquí el código javascript
|
||||
|
||||
@@ -1,5 +1,2 @@
|
||||
.seleccionado {
|
||||
background-color: yellow;
|
||||
font-weight: bold;
|
||||
}
|
||||
/* Agregar el código CSS necesario para el ejercicio */
|
||||
|
||||
|
||||
@@ -9,15 +9,6 @@
|
||||
<body>
|
||||
<h1>Ejercicio 3</h1>
|
||||
|
||||
<ul id="lista">
|
||||
<li>Elemento 1</li>
|
||||
<li>Elemento 2</li>
|
||||
<li>Elemento 3</li>
|
||||
<li>Elemento 4</li>
|
||||
<li>Elemento 5</li>
|
||||
<li>Elemento 6</li>
|
||||
</ul>
|
||||
|
||||
<script src="ejercicio3.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1 +1,35 @@
|
||||
// Agregar aquí el código javascript
|
||||
const btnSumar = document.querySelector("#sumar");
|
||||
const btnRestar = document.querySelector("#restar");
|
||||
|
||||
function actualizarVista(valor) {
|
||||
const MIN = 0;
|
||||
const MAX = 10;
|
||||
const parrafo = document.querySelector("#contador");
|
||||
parrafo.textContent = valor;
|
||||
|
||||
// Habilitamos o deshabilitamos los botones según el valor actual.
|
||||
// Cuando disabled es true, el botón no responde a clics.
|
||||
btnSumar.disabled = valor === MAX;
|
||||
btnRestar.disabled = valor === MIN;
|
||||
}
|
||||
|
||||
btnSumar.addEventListener("click", () => {
|
||||
// Obtenermos el valor actual
|
||||
let valor = parseInt(document.querySelector('#contador').textContent);
|
||||
// Sumamos 1
|
||||
valor++;
|
||||
// y actualizamos la vista
|
||||
actualizarVista(valor);
|
||||
});
|
||||
|
||||
btnRestar.addEventListener("click", () => {
|
||||
// Obtenermos el valor actual
|
||||
let valor = parseInt(document.querySelector('#contador').textContent);
|
||||
// Restamos 1
|
||||
valor--;
|
||||
// y actualizamos la vista
|
||||
actualizarVista(valor);
|
||||
});
|
||||
|
||||
// Llamamos a actualizarVista al inicio para establecer el estado inicial en 5.
|
||||
actualizarVista(5);
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Ejercicio 4</h1>
|
||||
<p>
|
||||
<button id="restar">Restar</button>
|
||||
<span id="contador">0</span>
|
||||
<button id="sumar">Sumar</button>
|
||||
</p>
|
||||
|
||||
<script src="ejercicio4.js"></script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user