<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="" method="post">
<p><label>账户:<input type="text" value="aaa"></label></p>
<p><label>密码:<input type="text"></label></p>
<p><input type="submit" value="登录"></p>
</form>
<script src="./jquery.js"></script>
<script>
// 1. 焦点事件
$('input[type="text"]').focusin(function (event) {
// 焦点刚刚进入时触发
console.log(event.type)
}).focus(function (event) {
// 已经获得焦点时触发
console.log(event.type)
}).focusout(function (event) {
// 刚要失去焦点时触发
console.log(event.type)
}).blur(function (event) {
// 已经失去焦点时触发
console.log(event.type)
});
// 2. change 事件
$('input[type="text"]').change(function (event) {
// 当输入框中的内容发生变化时触发
console.log(event.type)
}).on('input', function (event) {
// 当在输入框中输入数据时会实时触发,使用 val() 方法获取 input 框中的数据
console.log($(this).val())
// console.log(event.type)
}).select(function (event) {
// 当输入框中的内容被选中时触发
console.log(event.type)
});
// 3. submit 事件
$('form[method="post"]').on('submit', function(event){
// 表单被提交之后会自动地跳转页面(默认行为),跳转的目标是 action 属性中的URL,如果 action 属性为空,则把当前浏览器地址栏中的 URL 作为跳转的目标。
// 怎样阻止表单的默认行为(不提交数据,并且不跳转页面)
// 方法一:
event.preventDefault()
console.log('hahahah')
// 方法二:不常用,某些浏览器可能不支持这种写法
// return false;
})
</script>
</body>
</html>
jQuery---表单事件
最新推荐文章于 2024-08-08 07:22:03 发布