使用前提 npm install jquery 下载最新jq版本的jQuery
<script src = "放置jquery.js的路径"></script> 安装完成后导入jQuery
选择器(如何拿元素)
1.jQuery的选择器:
语法:$('需要获取的元素')
1-2.jQuery的筛选器
语法:$ ( '需要获取的元素' .筛选器() )
筛选器 | 解释 |
---|---|
first() | 取相同元素内第一个 |
late() | 取获取到相同元素的最后一个 |
eq(索引) | 从索引开始 如索填写的是4 则获取相同元素的第5个元素 |
next() | 拿到指定元后的元素 |
nextAll() | 拿到指定元素后的所有元素 |
prev() | 拿到指定元素前一个元素 |
prevAll() | 拿到指定元素前一个所有元素 |
parent() | 拿到除了指定元素外的所有标签元素 |
parents() | 从指定元素开始 逐层开始获取 |
siglings() | 拿到指定元素的所有的兄弟元素 |
find() | 拿到指定元素下的find()的指定元素(所有) |
操作元素
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>Test></title>
</head>
<body>
<div>
hellow word
<p>你好 世界</p>
</div>
<input type="text">
<script>
//1.html()
//等价与原生js中的innerHTML
//参数可以识别标签
console.log($('div').html('<h1>成功修改</h1>'))
//2.text
//等价于原生js的innerText
//不识别标签
console.log($('div').text("成功修改"))
//3.val
//等价与原生js中的value
//作用:用来设置表单value的值
console.log($('input').val('123456'))
</script>
</body>
</html>
、
操作类名
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>操作类名</title>
</head>
<body>
<div class="a b c d">
</div>
<button>切换</button>
<script>
//1. addClass()
//语法:元素集合.addClass(需要添加的类名)
$('div').addClass('e')
//2. removeClass()
//语法:元素集合。removeClass(需要删除的类名)
$('div').removeClass('b')
//3. toggleClass()
//如果本身有这个类名就是删除 没有就是添加
var get = document.querySelector('button')
get.onclick = function(){
$('div').toggleClass('box')
}
</script>
</body>
</html>
操作元素样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>操作元素样式</title>
</head>
<style>
div{
background-color: red;
width: 100px;
height: 100px;
}
</style>
<body>
<div>
</div>
<button>点我变大</button>
<button class="big">点我变的更大</button>
<script>
//css 设置样式
//语法:元素集合.css('样式名','样式值')
var btn = document.querySelector('button')
btn.onclick = function(){
$('div').css('width','300px')
$('div').css('height','300px')
}
//Or
var big = document.querySelector('.big')
big.onclick = function(){
$('div').css({
width:500,
height:500,
'background-color':'yellow'
})
}
</script>
</body>
</html>
操作元素属性
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>操作元素属性</title>
</head>
<body>
<div class="a" id="b">我是div标签</div>
<script>
//attr()
//作用:更改class or id 的属性值
$('div').attr('id','c')
//removeattr()
//作用:删除属性值
$('div').removeAttr('class')
//prop()
//作用和attr()一样 但只能修改prop自己创建的
</script>
</body>
</html>
获取元素尺寸
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>jquery获取元素尺寸</title>
</head>
<style>
*{
margin: 0px;
padding: 0px;
}
div{
width: 500px;
height: 500px;
padding: 20px;
border: 20px solid black;
background-color: skyblue;
background-clip: content-box;
}
</style>
<body>
<div></div>
<script>
//width and height 拿到元素的宽高
//获取内容的宽高
console.log($('div').width())
console.log($('div').height())
//innerWidth() innerHeight()
//拿到整个元素的宽高
console.log($('div').innerHeight())
console.log($('div').innerWidth())
//outerWidth(true) outerheight(true)
//拿到整个元素的 宽高 包含所有设置
console.log($("div").outerHeight(true))
console.log($("div").outerWidth(true))
</script>
</script>
</body>
</html>
获取元素偏移量
//offset() position
//速成的速度自己看ai吧
事件操作
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>事件绑定</title>
</head>
<style>
div{
background-color: red;
width: 200px;
height: 200px;
}
</style>
<body>
<div></div>
<script>
//on() 方法绑定事件
//语法:on('事件类型','事件处理函数')
$('div').on('click',function(){console.log('i am click even')})
//事件委托
//语法:on('事件类型','委托元素','事件处理函数')
//作用:把指定元素交给$()选择的元素来执行 可以是一个元素集合来选定on({}) 也就说批量绑定
//one()作用与on()是一样的 只不过只能执行一次
$('p').one({
click:function(){console.log('1')},
mouseover:function(){'鼠标移入事件'}
})
//hover()
//语法:元素集合.hover(移入时触发的函数,移出时触发的函数)
//当只写入一个函数时 移出移入都会触发
//常用事件函数
//click(),mouserover(),mouseout(),change(),......
//off()
//解除对应事件的处理函数
//trigger()事件触发
//使用代码的方式来触发
//语法:元素集合.trigger(事件类型)
setInterval(function(){
//表示1000ms触发一次绑定div的click事件
$('div').trigger('click')
},1000)
</script>
</body>
</html>
jquery基础动画效果
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>jquery基础动画绑定</title>
</head>
<style>
*{margin: 0px;padding: 0px;}
div{
width: 500px;
height: 500px;
background-color: red;
}
</style>
<body>
<button>show</button>
<button>hide</button>
<button>toggle</button>
<div></div>
<script>
$('button:nth-child(1)').click(function(){
//执行 show 动画函数
$('div').show(1000,'linear',function(){console.log('show触发了')})
})
$('button:nth-child(2)').click(function(){
//执行 hide 动画函数
$('div').hide(1000,'linear',function(){console.log('hide触发了')})
})
$('button:nth-child(3)').click(function(){
//执行 toggle 动画函数
$('div').toggle(1000,'linear',function(){console.log('toggle触发了')})
})
</script>
</body>
</html>
jquery折叠效果
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>jquery动画折叠效果</title>
</head>
<style>
*{margin: 0px;padding: 0px;}
div{
width: 500px;
height: 500px;
background-color: red;
}
</style>
<body>
<button>slideDown</button>
<button>slideUp</button>
<button>slideToggle</button>
<div></div>
<script>
$('button:nth-child(1)').click(function(){
//执行 slideDown 动画函数
$('div').slideDown(1000,'linear',function(){console.log('slideDown触发了')})
})
$('button:nth-child(2)').click(function(){
//执行 hide 动画函数
$('div').slideUp(1000,'linear',function(){console.log('slideUp触发了')})
})
$('button:nth-child(3)').click(function(){
//执行 toggle 动画函数
$('div').slideToggle(1000,'linear',function(){console.log('slideToggle触发了')})
})
</script>
</body>
</html>
jquery渐隐渐显效果
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>jquery的渐隐渐显效果</title>
</head>
<style>
*{margin: 0px;padding: 0px;}
div{
width: 500px;
height: 500px;
background-color: red;
}
</style>
<body>
<button>fadeIn</button>
<button>fadeOut</button>
<button>fadeToggle</button>
<button>fadeTo</button>
<div></div>
<!-- xxx动画函数('运动事件','指定透明度','运动曲线','运动回调函数') -->
<!-- 具体看fadTo() -->
<script>
$('button:nth-child(1)').click(function(){
//执行 slideDown 动画函数
$('div').fadeIn(1000,'linear',function(){console.log('fadeIn触发了')})
})
$('button:nth-child(2)').click(function(){
//执行 hide 动画函数
$('div').fadeOut(1000,'linear',function(){console.log('fadeOut触发了')})
})
$('button:nth-child(3)').click(function(){
//执行 toggle 动画函数
$('div').fadeToggle(1000,'linear',function(){console.log('fadeToggle触发了')})
})
$('div').fadeTo(1000,'0.66','linear',function(){console.log('运动到了指定的透明度')})
</script>
</body>
</html>
jquery综合动画效果
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>综合动画函数</title>
</head>
<style>
*{margin: 0px;padding: 0px;}
div{
width: 200px;
height: 200px;
background-color: skyblue;
position: absolute;
}
</style>
<body>
<button>开始 动画</button>
<div></div>
<script>
/*
综合动画
animate()
第一个参数:运动的样式 要以一个对象来实现
第二个参数:运动时间
第三个参数:运动曲线
第四个参数:运动结束回调函数
*/
$('button').click(function(){
$('div').animate({
width:400,
height:400,
left:200,
top:200,
'border-radius':'50%'
},1000,'linear',function(){ cosole.log('运动结束') })
})
</script>
</body>
</html>
jquery动画结束函数
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>动画结束效果</title>
</head>
<style>
*{margin: 0px;padding: 0px;}
div{
width: 500px;
height: 500px;
background-color: skyblue;
position: absolute;
}
</style>
<body>
<button>开始动画</button>
<button>停止(1)</button>
<button>停止(2)</button>
<div></div>
<script>
/*
结束动画
1.stop()
目前运动到什么位置就结束在什么位置
2.finish()
直接结束动画的结束位置
*/
$('button:nth-child(1)').click(function(){
$('div').toggle(2000)
})
$('button:nth-child(2)').click(function(){
$('div').stop()
})
$('button:nth-child(3)').click(function(){
$('div').finish()
})
</script>
</body>
</html>
jQuery的Ajax请求方式
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./node_modules/jquery/dist/jquery.js"></script>
<title>jQuery的Ajax请求方式</title>
</head>
<body>
<script>
/*
语法:$.ajax({配置项})
配置项:
1.url 必填 请求地址
2.method 选填 请求方式 默认get
3.data 选填 默认时'' 表示携带后端的参数
4.async 选填 默认时true 表示是否异步
5.success 选填 表示成功回调函数
6.error 选填 表示请求失败的回调函数
res表示后端的回调结果
*/
$.ajax({
url:'http://localhost:3306/item/one',
error:function(){
console.log('连接失败')
//......剩下的类推
}
})
</script>
</body>
</html>