01-jQuery属性操作
<!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>Document</title>
<script src="jquery.min.js"></script>
</head>
<body>
<a href="https://www.baidu.com/" title="都挺好">都挺好</a>
<input type="checkbox" name="" id="" checked>
<div index="1" data-index="2">我是div</div>
<span>123</span>
<script>
$(function() {
console.log($("a").prop("href"));
$("a").prop("title", "我们都挺好")
$("input").change(function() {
console.log($(this).prop("checked"));
})
console.log($("div").attr("index"));
$("div").attr("index", 4)
console.log($("div").attr("data-index"));
$("span").data("uname", "andy")
console.log($("span").data("uname"));
console.log($("div").data("index"));
})
</script>
</body>
</html>
02-jQuery内容文本值
<!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>Document</title>
<script src="jquery.min.js"></script>
</head>
<body>
<div>
<span>我是内容</span>
</div>
<input type="text" value="请输入内容">
<script>
console.log($("div").text());
$("div").text(123)
console.log($("input").val());
$("input").val(123)
</script>
</body>
</html>
03-返回指定祖先元素
<!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>Document</title>
<script src="jquery.min.js"></script>
</head>
<body>
<div class="one">
<div class="two">
<div class="three">
<div class="four"></div>
</div>
</div>
</div>
<script>
console.log($(".four").parents(".one"));
console.log($(".four").parents(".two"));
console.log($(".four").parents(".three"));
console.log($(".four").parents("body"));
</script>
</body>
</html>
04-jQuery遍历元素
<!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>Document</title>
<script src="jquery.min.js"></script>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<script>
$(function() {
var sum = 0;
var arr = ["red", "green", "blue"];
$("div").each(function(index, domEle) {
$(domEle).css("color", arr[index]);
sum = sum + parseInt($(domEle).text());
})
$.each({
name: "andy",
age: 18
}, function(i, ele) {
console.log(i);
console.log(ele);
})
})
</script>
</body>
</html>
05-创建添加删除元素
<!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>Document</title>
<script src="jquery.min.js"></script>
</head>
<body>
<ul>
<li>原先的li</li>
</ul>
<div class="test">原先的div</div>
<script>
$(function() {
var li = $(" <li>我是后来创建的li</li>");
$("ul").prepend(li);
var div = $("<div>我是后来创建的div</div>");
$(".test").before(div);
$("li").html("");
})
</script>
</body>
</html>
06-jQuery元素大小
<!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>Document</title>
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
padding: 10px;
border: 15px solid red;
margin: 20px;
}
</style>
<script src="jquery.min.js"></script>
</head>
<body>
<div></div>
<script>
$(function() {
console.log($("div").innerWidth());
console.log($("div").outerWidth());
console.log($("div").outerWidth(true));
})
</script>
</body>
</html>
07-jQuery位置
<!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>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
.father {
width: 400px;
height: 400px;
background-color: pink;
margin: 100px;
overflow: hidden;
position: relative;
}
.son {
width: 150px;
height: 150px;
background-color: purple;
position: absolute;
top: 10px;
left: 10px;
}
</style>
<script src="jquery.min.js"></script>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
<script>
$(function() {
console.log($(".son").position());
})
</script>
</body>
</html>
08-jQuery被卷去的头部
<!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>Document</title>
<style>
.back {
display: none;
position: absolute;
left: 1160px;
top: 1000px;
width: 80px;
height: 50px;
background-color: pink;
text-align: center;
line-height: 50px;
}
.container {
margin: 100px auto;
width: 800px;
height: 1200px;
background-color: purple;
}
</style>
<script src="jquery.min.js"></script>
</head>
<body>
<div class="back">返回顶部</div>
<div class="container"></div>
<script>
$(function() {
$(document).scrollTop(100);
var boxTop = $(".container").offset().top;
$(window).scroll(function() {
if ($(document).scrollTop() >= boxTop) {
$(".back").fadeIn()
} else {
$(".back").fadeOut()
}
$(".back").click(function() {
$("body,html").stop().animate({
scrollTop: 0
})
})
})
})
</script>
</body>
</html>