https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/change_event
当用户更改 <input>
、<select>
和 <textarea>
元素的值时,change 事件在这些元素上触发。
当标签的值被修改并且失去焦点后,但未提交时(例如:对<textarea>、 text、search、url、tel、email
或 password
类型的 <input>
元素进行编辑后)。
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text">
<script>
const input = document.querySelector('input')
// change事件是内容发生了变化触发
input.addEventListener('change', function () {
console.log('change事件发生了')
})
</script>
</body>
</html>