pregunta hola, acabo de entrar en JavaScript En el código de abajo, "preventDefault();" no se pudo ejecutar. Te agradecería mucho si pudieras decirme cuál es el problema. Ingresé el código "preventDefault" pero la página aún se vuelve a cargar. Pensé que lo había escrito bien, pero no sé dónde está el problema.
código ====html====
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="cstyle.css"> </head> <body> <div class="js-clock"> <h1>00:00</h1> </div> <form action="" class="js-form form showing"> <input type="text" placeholder="What is your name?"></form> <h4 class="js-greetings greetings"></h4> <script src="c.js"></script> <script src="greeting.js"></script> </body> </html>====js====
const form = document.querySelector(".js-form"), input = form.querySelector("input"), greeting = document.querySelector(".js-greetings"); const USER_LS = "currentUser", SHOWING_CN = "showing"; function saveName(text) { localStorage.setItem(USER_LS, text); } function handleSubmit(event) { event.preventDefault(); const currentValue = input.value; paintGreeing(currentValue); saveName(currentValue); } function askForName() { form.classList.add(SHOWING_CN); form.addEventListener( 'submit', handleSubmit); } function paintGreeing(text) { form.classList.remove(SHOWING_CN); greeting.classList.add(SHOWING_CN); greeting.innerText = `hello ${text}`; } function loadName() { const currentUser = localStorage.getItem(USER_LS); if (currentUser === null) { // she is not } else { paintGreeing(currentUser); // she is } } function init() { loadName(); } init();