jq入门教程
参考文档地址:
准备工作
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对象
var aa = $('选择器');
var bb = $(dom节点);
(一)绑定事件
1:$(选择器).on(事件类型,选择器,绑定函数);
2:时间托管
$(祖先选择器).on(事件类型, 选择器, 绑定函数);
$(祖先选择器).delegate(选择器, 事件类型, 绑定函数);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
ul {border: 1px solid;}
li {background-color: gray;margin-top: 15px;}
</style>
</head>
<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>
</script>
</body>
</html>
3:$(document).reday(fn); 相当于window.onload
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
// 在这里给button标签添加事件
</script>
</head>
<body>
<button>
登录
</button>
</body>
</html>
(二)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:$(选择器名称)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div id="app">app</div>
<div class="aaa">aaa</div>
<button>哈哈</button>
<ul>
<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元素并打印
// 选择.aaa元素并打印
// 选择ul后面的li元素并打印
$('button').on('click', function() {
alert(2222);
});
$('li').on('click', function() {
console.log(this);
})
</script>
</body>
</html>
ready()
<script>
$(document).ready(function() {
alert('页面加载完成');
// 需要页面加载完成 $('#app')才存在
$('#app').on('click', function() {
alert(2222);
})
})
</script>
2:find 和 children 都用来查找后代元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="box">
<div>adsfasdf</div>
<p>sfasdf</p>
<span>asdfa</span>
<span class="aa">asdfa</span>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
var $box = $('.box');
// 查找$box的后代元素,类名为.aa
$('.box').on('click', '.aa', function() {
// console.log(event.target);
console.log(this);
});
// 查找$box所有后代(子代)元素
var $children = $box.children();
console.log('$children', $children);
</script>
</body>
</html>
3: parents,closest 查找祖先元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="box">
<div>
<div class="aaa">
<p>
<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);
// 查找祖宗元素里离它最近的,类名为aaa的元素
var $aaa = $span.closest('.aaa');
console.log('$aaa', $aaa);
</script>
</body>
</html>
4:siblings 查找兄弟元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.active{color: red;}
li {cursor: pointer;}
</style>
</head>
<body>
<ul>
<li>第1个li元素</li>
<li class="active">第2个li元素</li>
<li>第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');
// 查找.active的所有兄弟元素
var $brothers = $active.siblings();
console.log('$brothers', $brothers)
</script>
</body>
</html>
(四)操作 html
1:$(选择器).html 相当于innerHTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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字符串并打印
//获取app里面的html字符窜
var str = $('#app').html();
console.log(str)
var str3 = `<li>第1个li元素</li>
<li>第2个li元素</li>
<li>第3个li元素</li>
<li>第4个li元素</li>`;
// 把str字符串添加到ul元素里
$('ul').html(str3);
</script>
</body>
</html>
2:$(选择器).text() 相当于innerText
3:$(选择器).val() 相当于input元素的.value
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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的值
let usename = $('#username').val();
console.log(usename)
// 设置#phone输入框的值
$('#phone').val(1234654654);
console.log($('#phone').val())
</script>
</body>
</html>
$(选择器).append() 相当于appendChild
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<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>
</html>
(五) 操作css
1:添加或移除一个类
- $(选择器).addClass(类名) 添加class;
$(选择器).removeClass(类名) 移除class;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>.red{color: red;}</style>
</head>
<body>
<div id="app">asdfasdffsf</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 给#app添加class red
$('#app').addClass('red');
// 给#app移除class red (3秒后移除red这个类名)
setTimeout(function(){
$('#app').remove('red')
},3000)
</script>
</body>
</html>
$(选择器).toggleClass(类名) // 有则去掉,无则加上
<!DOCTYPE html>
<html lang="en">
<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>
//本来就是点击事件toggle()
$('button').on('click',function(){
$('#app').toggleClass('dsn')
})
</script>
</body>
</html>
$(选择器).css() // 获取样式和设置样 ```javascript <!DOCTYPE html>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 获取字体大小
let font_size = $('.red').css('font-size')
// 设置样式:背景变灰色,添加边框,可以连着写
$('.red').css('backgroung-color','skyblue');
$('.red').css('border','1px solid red')
// 链式调用
setTimeout(function(){
$(‘.red’).css(‘background-color’, ‘green’).css(‘border’, ‘2px solid red’).css(‘font-size’, ‘26px’); })
<a name="NsPKf"></a>
## (六)操作属性
1. $(选择器).attr() 获取属性或者设置属性
```javascript
<!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>
</head>
<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地址
setTimgout(function(){
$('a').text('新娘');
$('a').attr('href','dfgdfgdfg')
})
</script>
</body>
</html>
- $(选择器).removeAttr() 删除属性 ```javascript <!DOCTYPE html>
<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>
3. $(选择器).prop() 操作属性 类似attr, 当attr不管用的时候使用prop
<a name="KQaBX"></a>
## (七) 效果
1. show() 显示和hide() 隐藏
```javascript
<!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>
</head>
<body>
<div>阿斯顿发的说法是对方</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 2秒后隐藏div
setTimeout(function(){
$('div').hide()
},2000)
// 1秒后显示div
setTimeout(function(){
$('div').show()
},1000)
</script>
</body>
</html>
- 滑动 ```javascript <!DOCTYPE html>
<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>
// 点击.box向上滑动
$(‘btn1’).on(‘click’,function(){
$('.box').slideUp();
})
// 点击.box向下滑动
$(‘btn1’).on(‘click’,function(){
$('.box').slideDown();
})
</script>
```
(八) 插件
https://www.jq22.com/jq1-jq3(一个日期插件示例)
(九) zepto.js
移动版的jquey