Files
notes_estom/JavaScript/index.js
2022-04-18 20:40:34 +08:00

32 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const my_form = document.querySelector('#my-form')
const name_input = document.querySelector('#name')
const email_input = document.querySelector('#email')
const msg = document.querySelector('.msg')
const user_list = document.querySelector('#users')
my_form.addEventListener('submit',onsubmit);
function onsubmit(e){
e.preventDefault();
if(name_input.value ==='' || email_input.value===''){
msg.classList.add('error')
msg.innerHTML='Please Enter all fields';
setTimeout(()=>{
msg.remove()
},3000)
// setTimeout本质上是一个异步的方法不会阻塞当前线程而是开启一个线程执行相关的访问非常有用
// 可以用来检测任务执行的状态,动态修改当前任务执行的内容。
}
else{
// console.log('success')
const li = document.createElement('li')
li.appendChild(document.createTextNode(`${name_input.value}:${email_input.value}`));
user_list.appendChild(li);
name_input.value=''
email_input.value=''
}
}