jq引入
什么是jQuery:
jQuery是一个简洁高效的且功能丰富的JavaScript工具库,是对原生JavaScript二次封装的工具函数集合
优点:
开源 | 简洁的选择器 | 简化的Ajax操作 | 良好的浏览器兼容 | 强大的链式操作
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jq引入</title>
<style>
.wrap {
background-color: green;
}
</style>
</head>
<body>
<div class="box">jq引入</div>
<div class="box">jq引入</div>
</body>
<script src="js/jquery-3.3.1.js"></script>
<script>
console.log(jQuery);
console.log($);
$('.box').text("呵呵").css("color", "red").addClass("wrap").on("click", function() {
alert("123");
})
</script>
</html>
jq选择器
$('css3选择器语法');
$('css3选择器语法').eq(index);
$('css3选择器语法').get(index);
$(js对象);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jq选择器</title>
<style>
body .box:first-child {}
</style>
</head>
<body>
<div class="box">123</div>
<div class="box">456</div>
</body>
<script src="js/jquery-3.3.1.js"></script>
<script>
console.log(jQuery);
console.log($);
var $boxs = $('.box');
console.log($boxs)
console.log($boxs.text())
var $box = $('.box:nth-child(1)');
console.log($box);
console.log($box.text());
console.log($box[0].innerText);
var box1 = $boxs[0];
console.log(box1);
var box2 = $boxs.get(1);
console.log(box2)
var $box1 = $(box1);
console.log($box1);
</script>
</html>
页面加载
window.onload = function() {
}
$(function(){
})
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>页面加载</title>
<script src="js/jquery-3.3.1.js"></script>
<!--在页面加载前的脚步语句中,如何获取页面对象 => 主动睡觉,页面一加载完毕,就醒 -->
<!--js中window提供了一个 window.onload = fn 事件: 页面结构及页面所需资源全部加载完毕,只能绑定一个事件方法-->
<!--jq中document提供了一个 $(document).ready(fn) 事件: 页面结构加载完毕,能绑定多个事件方法,可以简写-->
<script>
</script>
<script>
window.onload = function() {
console.log("window load 1");
};
window.onload = function() {
console.log("window load 2");
};
$(document).ready(function() {
console.log("document load 1");
});
$(function() {
console.log("document load 3");
});
</script>
<script>
$(function () {
$('.box').eq(1).text("000");
});
</script>
</head>
<body>
<div id="box" class="box">123</div>
<div class="box">456</div>
<img src="http://onehdwallpaper.com/wp-content/uploads/2015/11/Most-Beautiful-Girl-in-Flowers-Field.jpg"/>
</body>
<script>
$(function () {
var $box;
});
</script>
<script>
</script>
</html>
jq控制元素对象
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jq控制元素对象</title>
<style>
.abc {
background-color: red;
}
.box {
border-radius: 5px;
}
</style>
</head>
<body>
<div class="box">001</div>
<div class="box">002</div>
<div class="box">003</div>
<img src="" alt="" ooo="100w">
</body>
<script src="js/jquery-3.3.1.js"></script>
<script>
$(function () {
var res = $('.box:first-child').text("100").html("<b>888</b>");
res = $('.box').eq(1)
.css("color", "pink")
.css("font-size", "30px")
.css({
backgroundColor: "orange",
"height": "80px"
})
.css("width", function (index,oldVal) {
console.log(this);
console.log($(this).height());
return $(this).height() * 2;
})
.css("border-radius");
res = $('.box:nth-child(3)').addClass("abc").removeClass("abc");
console.log(res);
$('img').attr("src", "http://onehdwallpaper.com/wp-content/uploads/2015/11/Most-Beautiful-Girl-in-Flowers-Field.jpg")
.removeAttr("src");
console.log($('img').attr("ooo"));
})
</script>
</html>
jq获取盒子信息
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jq获取盒子信息</title>
<style>
body {
margin: 0;
}
.sup {
width: 350px;
height: 350px;
background-color: pink;
position: relative;
margin: 10px;
}
.box {
width: 200px;
height: 200px;
background-color: orange;
padding: 10px;
border: 20px solid black;
margin: 50px;
position: absolute;
top: 5px;
left: 5px;
}
</style>
</head>
<body>
<div class="sup">
<div class="box"></div>
</div>
</body>
<script src="js/jquery-3.3.1.js"></script>
<script>
var res = $('.box').css("padding");
console.log(res);
res = $('.box').width();
console.log(res);
res = $('.box').innerWidth();
console.log(res);
res = $('.box').outerWidth();
console.log(res);
res = $('.box').outerWidth(true);
console.log(res);
</script>
<script>
console.log($('.box').offset().top, $('.box').offset().left);
console.log($('.box').position().top, $('.box').position().left);
</script>
</html>
jq事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jq事件</title>
<style>
.sup {
width: 200px;
height: 200px;
background-color: red;
}
.sub {
width: 100px;
height: 100px;
background-color: orange;
}
</style>
</head>
<body>
<div class="box">
<span class="sss">123</span>
<i>456</i>
</div>
<div class="sup">
<div class="sub"></div>
</div>
</body>
<script src="js/jquery-3.3.1.js"></script>
<script>
var d = "AAA";
$('.box').on('click', 'span', {aaa: d}, function (ev) {
console.log(ev);
console.log(ev.data.aaa)
})
$('.sup').on('contextmenu', function (ev) {
ev.preventDefault();
var bgColor = $(this).css('background-color');
alert(bgColor);
})
$('.sup').click(function () {
console.log("父点击")
})
$('.sub').click(function (ev) {
ev.stopPropagation();
console.log("子点击")
})
</script>
</html>
jq动画
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jq动画(了解)</title>
<style>
.box {
width: 200px;
height: 200px;
background-color: orange;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
<script src="js/jquery-3.3.1.js"></script>
<script>
$('.box').on('mouseenter', function () {
$(this).animate({
width: "400px",
height: "400px",
"background-color": "red"
}, 1000, function () {
console.log("变大结束")
})
})
$('.box').on('mouseleave', function () {
$(this).animate({
width: "200px",
height: "200px"
}, 1000, function () {
console.log("动画完成时的回调函数")
})
})
</script>
</html>
jq结合css完成动画
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jq&css动画</title>
<style>
.box {
width: 200px;
height: 200px;
background-color: brown;
margin: 100px auto;
transition: 1s;
}
.box.move {
margin-top: 50px;
width: 300px;
height: 300px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
<script src="js/jquery-3.3.1.js"></script>
<script>
$('.box').on('mouseenter', function () {
$(this).addClass('move');
})
$('.box').on('mouseleave', function () {
$(this).removeClass('move');
})
</script>
</html>