jQuery
一个快捷的,轻便的JavaScript框架。
jquery支持css选择器。
jquery可以使用下载的js文件,也可以使用CDN。
https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js
例如:
设计思想
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document
</title>
</head>
<style>
</style>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<body>
<div class="div1">
</div>
</body>
<script>
$(function(){
$(".div1").css("background-color","red");
})
</script>
</html>
//获取奇数的li标签
$("li:even").css("background-color","red");
//获取偶数的li标签
$("li:odd").css("background-color","red");
//获取第二个li标签
$("li:eq(2)").css("background-color","red");
//以下的两种方法都可以改变li标签中class为box的css样式
$("li.box)").css("background-color","red");
$("li").filter('.box').css("background-color","red");
jquery的链式操作
$(function(){
$('h1').click(function(){
alter("单机");
}).css("background-clock",'red').mouseenter(
function(){
this.style
.backgroudColor
='bule';
}
).mouseout(function(){
this.style
.backgroudColor
='green';
})
})
取值赋值合体
$(function(){
alert($('#div').html())
$('#div').html("<h2>我是新赋值的内容</h2>")
})
Jquery的方法
filter not has 方法
$(function(){
$("div").filter('.box').css("background-color",'red');
$("div").has('.box').css("background-color",'red');
})
prev,next方法
<body
>
<h2
></h2
>
<p
></p
>
<span
></span
>
<h3
></h3
>
<em
></em
>
</body
>
<script
>
$(function(){
$("h3").prev().css("background-color",'red');
$("h3").next().css('background-color','blue');
$('ul').find('li').css('background-color','orange');
})
</script
>
index和eq方法
$(function(){
alert($('h3').index());
$('li').eq(3).css('background-color','pink');
$('li:eq(2)').css('background-color','pink');
})
attr
$(function(){
console
.log($('#idv').attr('title'));
console
.log($('#idv').attr('class'));
console
.log($('#idv').attr('id'));
$("#div").attr('class','box2');
$('#div').attr('title','world');
})
addClass,removeClass
$(function(){
$('#div1').addClass("box2 box3 box4");
$('#div1').removeClass('box2');
})
width innerWidth outerWidth
width() 直接是widthinnerWidth() width+paddingouterWidth() width+padding+borderouterWidth(true) width+border+padding+marginheight()innerHeight()outerHeight()
节点操作
inertBefore() before()
insertAfter() after()
appendTo() append()
prependTo() prepend()
remove()
$(function(){
$('span').insertBefore($('div'));
$('div').brefore('span')
$("div").insertAfter($('span'));
$('span').appendTo($('div'));
$('span').prependTo($('div'));
$("div").remove();
})
事件绑定
on off
on的用法
$(function(){
$("#div").on('click',function(){
alert('hello');
})
$('div').on("click mouseover",function(){
})
$('div').on({
click
:function(){
alert("点击");
},
mouseover
:function(){
alert("mouseover");
}
})
})
事件委托
$(function(){
$('ul').on("click",'li',function(){
$(this).css('background-color','red');
})
})
off的用法
$(function(){
//取消div1上的所有事件上的所有函数
$('#div1').off();
//取消某一事件下的所有函数
$('#div1').off('click');
//取消某一事件下的指定的函数
$('#div1').off('click',show);
})
ev.stopPropagation();阻止事件冒泡
ev.preventDefault();阻止默认行为