目录
6、拼接元素: p放到div中 p.appendTo(div)
一、DOM
1、获取div的html内容 div.html()
2、获取div的文本内容 div.text()
3、获取inp的value的值 inp.val()
4、创建元素 $("<p></p>")
5、拼接元素: p放到div中 div.append(p)
6、拼接元素: p放到div中 p.appendTo(div)
7、a.prepend(b)
<1>插入到最前面
<2>a是父级,b是子级
8、a.prependTo(b)
<1>插入到最前面
<2>a是子级,b是父级
9、把b插入到a后面 a.after(b)
10、把b插入到a前面 a.before(b)
11、把a插入到b后面 a.insertAfter(b)
12、把a插入到b前面 a.insertBefore(b)
13、用b替换a元素 a.replaceWith(b)
14、用a替换b a.replaceAll(b)
15、删除a元素 a.remove()
16、清空a的子元素 a.empty()
二、事件
1、绑定单击事件
$('#div1').click(function{ });
2、绑定鼠标移入事件
$('#div1').mousemove(function(){ });
3、$('#div1').on('click',function{ });
<1>绑定事件
<2>可同时添加多个事件
eg: $('#div1').on({
a: fun1,
b: fun2
});4、$('#div1').off();
<1>删除事件
<2>全部删除
<3>如果删除某一个事件括号传入指定值
5、注意:敲重点
<1>mouseover mouseout 与 mouseenter mouseleave的区别:
1)over与out会造成多次无效的触发
2)enter与leave鼠标移入不会进行触发,建议使用enter与leave
<2>复合方法
1)ready
2)hover(fn1,fn2)
<3>事件对象
1)默认传参的e (这个是变量名,啥都可以,见名知意)
2)pageX ===clientX
3)pageY ===clientY
4)点击元素:traget
5)绑定元素:currentTarget
6)获取时间戳:timeStamp
7)类型:type
<4>注意:JQ不支持滚轮事件
<5>阻止冒泡:e.stopPropagation()
<6>阻止默认:preventDefault()
<7>既阻止冒泡,也阻止默认:return false
三、动画
1、简单动画
(1)显示:show()
(2)隐藏:hide()
(3)显示或隐藏:toggle()
(4)向下展开:slideDown()
(5)向上收起:slideUp()
(6)展开或收起:slideToggle
(7)淡入:fadeln()
(8)淡出:fadeOut()
(9)渐变到一定程度:fadeTo()
(10)淡入或淡出:fadeToggle()
注意:上面小括号可以传递两个参数
1. 事件
2. 回调函数 时间
<1>毫秒
<2>有默认的三个值
1)show
2)normal
3)fast
2、任意动画
(1)原生jq动画,不支持颜色
d.animate({
left:'50px',
top: '50px'
},2000)(2)具有累加累减的功能:left: "+=100px"
(3)同步动画:同时改变的
(4)列队动画
<1>回调:d.animate({},fn(){})
<2>连缀:d.animate().animate().animate()
(5)动画控制
<1>暂停:stop()
<2>延迟:delay()
<3>结束:finish()
3、jquery动画拓展库jQuery.easing
具体案例代码:
<!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>jq</title>
<style>
section{
width: 100px;
height: 100px;
background-color: gold;
}
</style>
</head>
<body>
<ul>
<li>
<span>ul item1</span>
</li>
<li>ul item2</li>
<li class="yes">ul item3</li>
<li>ul item4</li>
<li>ul item5</li>
</ul>
<ol>
<li>ol item1</li>
<li class="yes">ol item2</li>
<li>ol item3</li>
<li>ol item4</li>
<li>ol item5</li>
</ol>
<p></p>
<button>点我</button>
<section></section>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
// var p = document.getElementsByTagName("p")[0];
// p.innerHTML = "上山打老虎"
// p.style.color = "red";
// $("p").html("上山打老虎")
// .css("color","red")
// dom与jq可以互换
// var dOK = document.getElementById("ok");
// console.log($(dOK));
// console.log($("#ok")[0]);
// window.onload = function(){
// dom0方法,只能实现一次
// }
// $(document).ready(function(){
// dom2方法,可以实现多次,还可以简写
// })
// $(function(){
// 上面ready方法的简写
// })
// onload方法与ready方法的区别是什么?
$("ol li:nth-of-type(2)").css({
color: "red",
fontSize: 26
// 'font-size': 26
}).attr("id","ok").toggleClass("yes")
$("ul li:eq(3)").css({
color: "green"
})
// 非破坏性操作:方法返回自身(this指向原来的jq对象)
// 破坏性操作:this指向新的jq对象
// var a = $("ul li").filter(".yes").end().css({
// color: "pink"
// }).html()
// console.log(a);
// $("p").html("<a href='###'>123123</a>")
// $("ul").remove()
$("p").click(function(){
console.log(111)
}).hover(function(){
console.log(222)
}, function(){
console.log(333)
})
$("button").click(function(){
// $("section").hide()
// $("section").fadeToggle(3000,function(){
// // 动画执行结束后,执行这个方法
// alert("动画结束了")
// })
// 使用回调
// $("section").animate({
// width: 200,
// // height: 200
// },300,function(){
// // alert("ok")
// $("section").animate({
// height: 200
// }, 300)
// })
$("section").animate({width: 200},300)
.animate({height: 300},300)
})
</script>
</html>
运行结果:
点击按钮前: 点击按钮后: