jQuery 入门教程
参考文档地址:
准备工作
1.jQuery介绍
- 是一个用js封装的库, 专用用来方便我们操作dom节点的
- $(‘#app’).html(); // 获取id为app的元素的节点的内容(包含标签)
2. 导入jQuery.js, 使用本地 js 或 cdn 地址都可以
https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
3. 创建jQuery对象
<body>
<div id="app">asdfasdf</div>
<script>
var aa = $(选择器); // 方法1
// 创建jQuery对象
// 方法1 $(选择器)
// var obj = {};
// var date = new Date();
var $app = $('#app');
console.log('$app', $app);
var bb = $(dom节点) // 方法2
// 方式2 $(原生dom节点)
var app = document.querySelector('#app');
console.log('app', app);
var $app = $(app);
</script>
</body>
(一) 绑定事件
1. $(选择器).on(事件类型,绑定函数);
<style>
.box {
background-color: gray;
padding: 50px;
}
</style>
<body>
<div class="box">
<button>按钮</button>
</div>
<ul>
<li>xxx1</li>
<li>xxx2</li>
<li>xxx3</li>
</ul>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 给button绑定事件, 第一个参数是事件类型, 第二参数是绑定的函数(回调函数)
$('button').on('click', function() {
alert(2222);
});
$('li').on('click', function() {
console.log(this);
})
</script>
</body>
2. 事件托管
$(祖先选择器).on(事件类型, 选择器, 绑定函数);
$(祖先选择器).delegate(选择器, 事件类型, 绑定函数);
<style>
ul {
border: 1px solid;
}
li {
background-color: gray;
margin-top: 15px;
}
</style>
<body>
<ul>
<li>1.xxxx</li>
<li>2.xxxx</li>
<li>3.xxxx</li>
<li>4.xxxx</li>
<li>5.xxxx</li>
</ul>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 把li的事件托管到ul上, $(祖先选择器).on(事件类型, 选择器, 绑定函数);
$('ul').on('click', 'li', function() {
// console.log(event.target);
console.log(this);
});
</script>
</body>
3. $(document).ready(fn); 相当于window.onload
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function() {
alert('页面加载完成');
// 需要页面加载完成 $('#app')才存在
$('#app').on('click', function() {
alert(2222);
})
})
</script>
<body>
<div id="app">
asdfasdfasdf
</div>
</body>
(二) ajax 请求
1. get 请求: https://jquery.cuishifeng.cn/jQuery.get.html
2. post 请求
3. ajax 请求
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$.ajax({
type: 'get',
url: 'http://huruqing.cn:3000/api/film/getList2',
data: {},
dataType: 'json',
success: function(res) {
console.log(res);
},
// 网络问题,地址不对等原因导致前端跟后台都没有建立成功的通信
error: function() {
alert('请求错误');
}
})
</script>
(三) 选择器
1. $(选择器名称)
<body>
<div id="app">app</div>
<div class="aaa">aaa</div>
<button>哈哈</button>
<button>哈哈</button>
<button>哈哈</button>
<button>哈哈</button>
<button>哈哈</button>
<ul id="list1">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul id="list2">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 选择#app元素并打印
$('#app');
// 选择.aaa元素并打印
$('.aaa');
$('button');
// 选择ul后面的li元素并打印
$('#list1 li').on('click', function() {
console.log(this);
});
</script>
</body>
2. find 和 children 都用来查找后代元素
<body>
<div class="box">
<div>adsfasdf</div>
<p>sfasdf</p>
<span>asdfa</span>
<div>
<span class="aa">asdfa</span>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
var $box = $('.box');
// 查找$box的后代元素, 类名为aaa
var $aa = $box.find('.aa');
console.log('$aa', $aa);
// 查找$box所有子代元素
var $children = $box.children();
console.log('$children', $children);
</script>
</body>
3. parents,closest 查找祖先元素
<body>
<div class="box">
<div class="aaa">
<div>
<p class="aaa">
<span>后代元素</span>
</p>
</div>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
var $span = $('span');
// 查找span元素的所有的祖宗元素
var $parents = $span.parents();
console.log('$parents', $parents);
// 查找span元素祖宗元素里离它最近的类名为aaa的元素
var $aaa = $span.closest('.aaa');
console.log('$aaa', $aaa);
</script>
</body>
4. siblings 查找兄弟元素
<head>
<style>
.active {
color: red;
}
li {
cursor: pointer;
}
</style>
</head>
<body>
<ul>
<li>第1个li元素</li>
<li>第2个li元素</li>
<li class="active">第3个li元素</li>
<li>第4个li元素</li>
</ul>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
var $active = $('.active');
// 查找.acitve的兄弟元素
var $brothers = $active.siblings();
console.log('$brothers', $brothers);
</script>
</body>
(四) 操作 html
1. $(选择器).html() 相当于innerHTML
<head>
<style>
.active {
color: red;
}
li {
cursor: pointer;
}
</style>
</head>
<body>
<div id="app">
<div>dfasdfadsf</div>
<div>dfasdfadsf</div>
<div>dfasdfadsf</div>
</div>
<ul>
</ul>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 获取app里面的html字符串
var str = $('#app').html();
console.log('str1', str);
var str2 = $('#app').text();
console.log('str2', str2);
var str3 = `<li>第1个li元素</li>
<li>第2个li元素</li>
<li>第3个li元素</li>
<li>第4个li元素</li>`;
// 将str3字符串添加ul里面
$('ul').html(str3);
</script>
</body>
2. $(选择器).text() 相当于innerText
同上,不再举例
3. $(选择器).val() 相当于input元素的.value
<head>
<style>
.active {
color: red;
}
li {
cursor: pointer;
}
</style>
</head>
<body>
<div id="app">
<input id="username" type="text" value="huruqing"><br/>
<input id="phone" type="text">
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 读取输入框#username的值
var username = $('#username').val();
console.log('username', username);
// 设置#phone输入框的值
$('#phone').val(13811112222);
</script>
</body>
4. $(选择器).append() 相当于appendChild
<body>
<ul>
<li>xxx</li>
<li>xxx</li>
<li>xxx</li>
</ul>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 在ul后面再添加一条数据
$('ul').append('<li>我是新来的</li>');
</script>
</body>
(五) 操作 css
1. 添加或移除一个类
- $(选择器).addClass(类名) 添加class;
- $(选择器).removeClass(类名) 移除class;
<head>
<style>
.red {
color: red;
}
</style>
</head>
<body>
<div id="app" class="flex f12">asdfasdffsf</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 添加class red
$('#app').addClass('red');
// 移除class red
setTimeout(function() {
$('#app').removeClass('red');
}, 2000)
</script>
2. $(选择器).toggleClass(类名) // 有则去掉,无则加上
<head>
<meta charset="UTF-8">
<style>
.dsn {
display: none;
}
</style>
</head>
<body>
<button>点击</button>
<div id="app">
<p> asdfasdfasdfasdfasdf </p>
<p> asdfasdfasdfasdfasdf </p>
<p> asdfasdfasdfasdfasdf </p>
<p> asdfasdfasdfasdfasdf </p>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$('button').on('click', function() {
// 若#app没有dsn类就加上,有则移除
$('#app').toggleClass('dsn');
})
</script>
</body>
3. $(选择器).css() // 获取样式和设置样
<head>
<style>
.red {
color: red;
font-size: 20px;
}
</style>
</head>
<body>
<div class="red">asdfasdffsf</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 获取字体大小
var size = $('.red').css('font-size');
console.log('font-size', size);
// 设置样式:背景变灰色,添加边框,可以连着写
$('.red').css('background-color', 'gray');
$('.red').css('border', '2px solid blue');
// 链式调用
setTimeout(function() {
$('.red').css('background-color', 'green').css('border', '2px solid red').css('font-size', '26px');
}, 2000)
</script>
(六) 操作属性
1. $(选择器).attr() 获取属性或者设置属性
<body>
<a href="http://www.baidu.com">百度</a>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 获取a标签的href属性
var href = $('a').attr('href');
console.log('href', href);
// 2秒后把百度换成新浪,同时改变它的href地址
setTimeout(function() {
$('a').text('新浪');
$('a').attr('href', 'http://sina.com');
}, 2000)
</script>
</body>
2. $(选择器).removeAttr() 删除属性
<body>
<a href="http://www.baidu.com">百度</a>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 两秒后删除属性href
setTimeout(function() {
$('a').removeAttr('href');
}, 2000)
</script>
</body>
3. $(选择器).prop() 操作属性 类似attr, 当attr不管用的时候使用prop
<body>
<input type="radio" checked>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
var flag = $('input').attr('checked');
</script>
</body>
(七) 效果
1. show() 显示和hide() 隐藏
<body>
<div class="box">
<p>sadfasdfasdfasdfasdfasdf</p>
<p>sadfasdfasdfasdfasdfasdf</p>
<p>sadfasdfasdfasdfasdfasdf</p>
<p>sadfasdfasdfasdfasdfasdf</p>
<p>sadfasdfasdfasdfasdfasdf</p>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 2秒后隐藏div
setTimeout(function() {
$('.box').hide();
}, 2000);
// 1秒后显示div
setTimeout(function() {
$('.box').show();
}, 3000)
</script>
2. 滑动 上 slideUp() 下 slideDown()
<body>
<div class="box">
<p>这是一个段落。</p>
<p>这是一个段落。</p>
<p>这是一个段落。</p>
<p>这是一个段落。</p>
<p>这是一个段落。</p>
<p>这是一个段落。</p>
<p>这是一个段落。</p>
</div>
<button class="btn1">上滑</button>
<button class="btn2">下滑</button>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
// 点击.btn1向上滑动
$('.btn1').on('click', function() {
$('.box').slideUp();
})
// 点击.btn2向下滑动
$('.btn2').on('click', function() {
$('.box').slideDown();
})
</script>
</body>
3. 淡入淡出 https://www.runoob.com/jquery/jquery-fade.html
(八) 插件
https://www.jq22.com/jq1-jq3
一个日期插件示例
<head>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script src="./date-plugin/zane-calendar.min.js"></script>
<style>
.box {
width: 600px;
height: 600px;
margin: 0 auto;
padding: 50px;
}
</style>
</head>
<body>
<input type="text" name="" id="date">
<script>
zaneDate({
elem: '#date'
});
</script>
</body>
(十) zepto.js
移动版的jquey
(十一) underscore.js 和 loadash js 库
(十二) 重写计算器
- 引入jQuery.js
- 实现点击数字, 数字显示在输入框
(1) 绑定事件: 使用事件托管
(十三) 重写todoList
- 自定义属性
```xxxx
// 获取自定义属性 $(‘p’).data(‘index’); ```