一、jQuery 概述
二、jQuery的基本使用
$('div').hide();
//1. 等待页面DOM加载完毕再去执行js 代码
// $(document).ready(function() {
// $('div').hide();
// })
// 2. 等待页面DOM加载完毕再去执行js 代码
$(function(){
$('div').hide();
})
三、jQuery常用API
1.jQuery选择器
$(function(){
$('.nav>li').mouseover(function(){
$(this).children('ul').show()
})
$('.nav>li').mouseout(function(){
$(this).children('ul').hide()
})
})
$(function(){
$('#left li').mouseenter(function(){
var index = $(this).index()
// $('#content div').eq(index).show();
// 4. 让他的兄弟隐藏起来
// $('#content div').eq(index).siblings().hide()
// 链式编程!!!!!!!!!!!!!!!!!!!
$('#content div').eq(index).show().siblings().hide()
})
})
2.jQuery样式操作
<div class="tab_list">
<ul>
<li class="current">商品介绍</li>
<li>商品参数</li>
<li>商品评价</li>
</ul>
</div>
<div>
<div class="item" style="display: block;">
商品介绍
</div>
<div class="item">
商品参数
</div>
<div class="item">
商品评价
</div>
</div>
<script>
$(function(){
$('li').click(function(){
$(this).addClass('current').siblings().removeClass('current');
var index = $(this).index()
$('.item').eq(index).show().siblings().hide()
})
})
</script>
3.jQuery效果
<div class="tab_list">
<ul>
<li class="current">商品介绍</li>
<li>商品参数</li>
<li>商品评价</li>
</ul>
</div>
<div>
<div class="item" style="display: block;">
商品介绍
</div>
<div class="item">
商品参数
</div>
<div class="item">
商品评价
</div>
</div>
<script>
$(function(){
$('li').click(function(){
$(this).addClass('current').siblings().removeClass('current');
var index = $(this).index()
$('.item').eq(index).show(1000,"linear",()=>{
console.log('显示');
}).siblings().hide(1000,'linear',()=>{
console.log('隐藏');
})
})
})
$(function(){
$('button').hover(()=>{
$('div').slideDown(1000,'linear')
},()=>{
$('div').slideUp(1000,'linear')
})
})
//简单写法
$(function(){
$('button').hover(()=>{
$('div').slideToggle(1000,'linear')
})
})
//纯CSS实现!!!!!!!!不能加display属性了不过
<style>
div {
height: 0;
background-color: pink;
transition: height 1s linear;
}
button:hover + div {
height: 100px;
}
</style>
</head>
<body>
<button>asd</button>
<div></div>
$(function(){
$('button').hover(()=>{
//stop()方法必须写在动画前
$('div').stop().slideToggle(1000,'linear')
})
})
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kZ3PSufZ-1628410202203)(C:\Users\KAN\AppData\Roaming\Typora\typora-user-images\image-20210718202134389.png)]
4.jQuery属性操作
<body>
<a href="https://www.baidu.com" title="都挺好">链接</a>
<input type="checkbox" name="" id="" checked>
<div index='1' data-index="24">我是div</div>
<span index="1">234</span>
<script>
$(function(){
// 1. element.prop("属性名") 获取元素固有的属性
console.log($('a').prop('href'));
$("a").prop("title","我们都挺好");
$("input").change(function(){
console.log($(this).prop('checked'));
})
// 不是元素固有属性,这里不能用prop()去获取
// console.log($("div").prop("index"));
// 2.元素的自定义属性 我们通过 attr();
console.log($('div').attr("index"));
$('div').attr("index",4)
console.log($('div').attr("data-index"));
// 3.数据缓存 data() 这个里面的数据是存放在元素的内存里面
$("span").data("uname","andy") //存在了data-uname里
console.log($("span").data("uname")); // 获取了 data-uname 里的数据
//这个方法获取data-index h5自定义属性 第一个 不用写 data- !!! 而且返回的是数字型
console.log($("div").data("index")) // 获取了 data-index 里的数据
})
</script>
</body>
<div class="check_box">
<div>顶部全选<input type="checkbox" name="" class="check_all"></div>
<ul>
<li>子项<input type="checkbox" class="item_check"></li>
<li>子项<input type="checkbox" class="item_check"></li>
<li>子项<input type="checkbox" class="item_check"></li>
</ul>
<div>底部全选<input type="checkbox" class="check_all"></div>
</div>
</body>
<script>
// 1.全选 全不选功能模块
// 就是把全选按钮的状态赋值给 三个小的按钮就可以了
$(function(){
$('.check_all').change(function(){
var checked = $(this).prop('checked')
// console.log(checked);
$('.item_check, .check_all').prop('checked',checked)
})
})
// 2. 如果小复选框被选中的个数等于3 就应该把全选按钮选上,否则全选按钮不选
$(function(){
$('.item_check').change(function(){
//if (被选中的复选框的个数 === 3){
// 就要选中全选按钮
// } else {
// 不要选中全选按钮
// }
// console.log($('.item_check:checked').length);
if ($('.item_check:checked').length === $(".item_check").length ) {
$('.check_all').prop('checked',true);
} else {
$('.check_all').prop('checked',false);
}
})
})
</script>
5.jQuery文本属性值
6.jQuery元素操作
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<script>
$(function(){
var sum = 0;
var arr = ["red","green","blue"]
$('div').each(function(index,domElm){
// console.log(i);
// 回调函数的第二个参数一定是 dom元素对象
// console.log(domElm);
// domElm,css('color'); dom元素对象没有css方法
$(domElm).css('color',arr[index])
sum += parseInt($(domElm).text())
})
console.log(sum);
})
</script>
</body>
<script>
$(function(){
var sum = 0;
var arr = ["red","green","blue"]
$('div').each(function(index,domElm){
// console.log(i);
// 回调函数的第二个参数一定是 dom元素对象
// console.log(domElm);
// domElm,css('color'); dom元素对象没有css方法
$(domElm).css('color',arr[index])
sum += parseInt($(domElm).text())
})
// console.log(sum);
$.each($("div"),function(i,ele){
console.log(i);
console.log(ele);
});
// each 也能遍历对象和数组哦
$.each(arr,function(i,e){
console.log(i);
console.log(e);
})
})
<body>
<ul>
<li>
原先
</li>
</ul>
<div class="text">原来div</div>
<script>
$(function () {
// 1. 创建元素
var li = $("<li>后来</li>")
// 2.添加元素
// (1) 内部添加
//$('ul').append(li) //内部添加并且放到内容的最后面
$('ul').prepend(li) //内部添加并且放到内容的最前面 注意一个变量只能添加一次,不能同时添加到前或后
// (2)外部添加
var div = $('<div>我是后面添加的div</div>');
$(".text").after(div)
$(".text").before(div)
// 3.删除元素
$("ul").remove(); // 自己都会杀掉
$("ul").empty(); // 可以删除匹配元素里面的子节点 孩子
$("ul").html('') // 同第二个效果一样
})
</script>
</body>
7.jQuery尺寸、位置操作
四、jQuery事件
1.JQuery事件注册
2.JQuery事件处理
$(function(){
// 1. 单个事件注册
// $('div').click(function(){
// $(this).css("background","purple")
// })
// $('div').mouseenter(function(){
// $(this).css("background","green")
// })
// 2. 事件处理ON
// $('div').on({
// mouseenter:function(){
// $(this).css("background","red")
// },
// mouseleave:function(){
// $(this).css("background","purple")
// },
// click:function(){
// $(this).css("background","green")
// }
// })
$('div').on("mouseenter mouseleave",function(){
$(this).toggleClass("current")
})
})
$(function(){
// 1. 单个事件注册
// $('div').click(function(){
// $(this).css("background","purple")
// })
// $('div').mouseenter(function(){
// $(this).css("background","green")
// })
// 2. 事件处理ON
// $('div').on({
// mouseenter:function(){
// $(this).css("background","red")
// },
// mouseleave:function(){
// $(this).css("background","purple")
// },
// click:function(){
// $(this).css("background","green")
// }
// })
// $('div').on("mouseenter mouseleave",function(){
// $(this).toggleClass("current")
// })
// (2) on可以实现事件委托(委派)
// $("ul li").click();
// $('ul').on('click',"li",function(e){
// console.log(e);
// })
// (3) on 可以给动态创建的元素绑定事件
// $('ol li').click(function(){
// alert(11) //无效果
// })
$("ol").on('click',"li",function(){
alert(1); // 有效果
})
var li = $('<li>我是后来创建的</li>')
$("ol").append(li)
})
$(function () {
// 1. 添加小li
$(".btn").on("click", function () {
var li = $("<li></li>");
li.html($(".txt").val() + "<a href='javascript:;'>删除</a>");
$('ul').prepend(li)
li.slideDown();
$(".txt").val('')
})
// 2.删除小li
// 这样不能给未来出现的li添加事件
// $("ul li a").click(function () {
// alert(01)
// })
// 可以给动态创建的元素添加事件
$('ul').on('click','a',function(){
$(this).parent().slideUp(function(){
$(this).remove();
});
})
})
3.JQuery事件对象
五、jQuery其他方法
1.jQuery拷贝对象
2.多库共存
3.jQuery插件
ToDoList案例
![image-20210724231507552](https
]
3.JQuery事件对象
五、jQuery其他方法
1.jQuery拷贝对象
2.多库共存
3.jQuery插件
ToDoList案例