Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="it">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <link rel="stylesheet"
- href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
- </head>
- <body>
- <div class="container mt-5">
- <h1 class="mb-4">Gestione Paragrafi</h1>
- <button id="inserisci" class="btn btn-primary mb-3">Inserisci Paragrafo</button>
- <button id="elimina" class="btn btn-danger mb-3">Elimina Paragrafo</button>
- <div id="container_paragrafi">
- <!-- qui saranno inseriti i paragrafi -->
- </div>
- </div>
- <script
- src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
- </script>
- <script>
- let conta_paragrafi = 0;
- document.getElementById('inserisci').addEventListener('click', function() {
- const container_paragrafi = document.getElementById('container_paragrafi');
- const nuovo_paragrafo = document.createElement('p');
- conta_paragrafi++;
- nuovo_paragrafo.textContent = conta_paragrafi + ' creat' +
- (conta_paragrafi > 1 ? 'i' : 'o');
- // nuovo_paragrafo.className =
- // conta_paragrafi%2===0 ? 'alert alert-info ' : 'alert alert-warning';
- nuovo_paragrafo.className =
- conta_paragrafi%2===0 ? 'text-primary' : 'text-danger';
- container_paragrafi.appendChild(nuovo_paragrafo);
- });
- document.getElementById('elimina').addEventListener('click', function() {
- let paragrafi = document.getElementById('container_paragrafi').children;
- if (paragrafi.length>0) {
- paragrafi[0].remove();
- conta_paragrafi--;
- paragrafi = document.getElementById('container_paragrafi').children;
- for (let i=0; i<paragrafi.length; i++) {
- paragrafi[i].textContent = (i+1) + ' creat' + (i>0 ? 'i' : 'o');
- //paragrafi[i].className = (i+1)%2===0 ? 'alert alert-info' : 'alert alert-warning';
- paragrafi[i].className = (i+1)%2===0 ? 'text-primary' : 'text-danger';
- }
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement