22-作业二:jquery与dom
Js(放js的文件夹)
jquery-3.6.1.js(家人们自己上网上下载,实在找不到可以私我要)
01_jquery入门使用.html
<!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="js/jquery-3.6.1.js"></script>
</head>
<body>
<div id="box1">box1</div>
<div id="box2">box2</div>
</body>
<script>
var box1 = $("#box1");
var box2 = $("#box2");
// console.log(box1.html());
alert(box1.html());
</script>
</html>
02_jquery事件绑定.html
<!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>事件绑定</title>
<script src="js/jquery-3.6.1.js"></script>
</head>
<body>
<input type="button" value="点我" id="btn">
</body>
<script>
// var btn = document.getElementById("btn");
// btn.onclick = function(){
// alert("我被点击了");
// }
$("#btn").click(function(){
alert("我被点击了");
});
</script>
</html>
03_jquery入口函数.html
<!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="js/jquery-3.6.1.js"></script>
<script>
// window.onload = function (){
// //javascript代码
// }
//jquery入口函数(dom文档执行完成后执行的代码)
$(function(){
$("#btn").click(function(){
alert("hello");
});
});
</script>
</head>
<body>
<input type="button" value="点我" id="btn">
</body>
</html>
04_设置css样式.html
<!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="js/jquery-3.6.1.js"></script>
<script>
$(function(){
$("#box1").css("width","200px");
$("#box1").css("height","200px");
$("#box1").css("backgroundColor","red");
$("#box1").css("border","1px solid black");
});
</script>
</head>
<body>
<div id="box1">box1</div>
</body>
</html>
05_基本选择器.html
<!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: 100px;
height: 100px;
border: 1px solid skyblue;
}
</style>
<script src="js/jquery-3.6.1.js"></script>
<script>
$(function(){
$("#btn1").click(function(){
$("#box1").css("backgroundColor","green");
});
$("#btn2").click(function(){
$(".box2").css("backgroundColor","red");
});
$("#btn3").click(function(){
$("div").css("backgroundColor","yellow");
});
$("#btn4").click(function(){
$("div").css("border","1px solid black");
$("input").css("border","1px solid black");
});
});
</script>
</head>
<body>
<div id="box1">box1</div>
<div id="box2" class="box2">box2</div>
<div id="box3" class="box2">box3</div>
<div id="box4">box4</div>
<div id="box5">box5</div>
<br>
<input type="text" id="username">
<br>
<input type="button" value="将id为box1的元素的背景改成green" id="btn1"><br>
<input type="button" value="将class为box2的元素的背景改成red" id="btn2"><br>
<input type="button" value="将div都变成黄色" id="btn3">
<input type="button" value="将div和input的borde变成1px solid black" id="btn4">
</body>
</html>
06_层级选择器.html
<!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>层级选择器</title>
<style>
div{
border: 1px solid black;
}
#box1{
width: 500px;
height: 500px;
background-color: green;
}
#box2{
width:200px;
height: 200px;
background-color: yellow;
}
#box3{
width: 220px;
height: 220px;
background-color: pink;
}
#box4{
width: 60px;
height: 60px;
background-color: red;
}
</style>
<script src="js/jquery-3.6.1.js"></script>
<script>
$(function(){
$("#btn1").click(function(){
$("#box1 div").css("backgroundColor","skyblue");
});
$("#btn2").click(function(){
$("#box1>div").css("backgroundColor","black");
});
});
</script>
</head>
<body>
<div id="box1">
box1
<div id="box2">box2</div>
<div id="box3">box3
<div id="box4">box4</div>
</div>
</div>
<br>
<input type="button" value="将div1的所有后代元素背景都改成skyblue" id="btn1">
<input type="button" value="将div1的所有子元素背景都改成black" id="btn2">
</body>
</html>
07_属性选择器.html
<!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>属性名称选择器</title>
<script src="js/jquery-3.6.1.js">
</script>
<script>
$(function () {
$("#btn1").click(function(){
$("div[title]").css("backgroundColor","green");
});
$("#btn2").click(function(){
$("div[title='ss']").css("backgroundColor","green");
});
$("#btn3").click(function(){
$("input[name='hobby'][value='basketball']").css("backgroundColor","skyblue");
});
});
</script>
<style>
div {
width: 100px;
height: 100px;
border: 1px solid black;
}
</style>
</head>
<body>
<div title="ss">div1</div>
<div title="aa">div2</div>
<hr>
爱好:<br>
<input type="text" name="hobby" value="basketball"><br>
<input type="text" name="hobby" value="football"><br>
<input type="button" value="将有title属性的div背景改为green" id="btn1">
<input type="button" value="将title属性为ss的div背景改为green" id="btn2">
<input type="button" value="将输入框name为hobby且value为basketball的背景改为skyblue" id="btn3">
<hr>
</body>
</html>
08_过滤选择器.html
<!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="js/jquery-3.6.1.js"></script>
<script>
$(function () {
$("#btn1").click(function(){
$("div:first").css("color","red");
});
$("#btn2").click(function(){
$("div:last").css("color","red");
});
$("#btn3").click(function(){
$("div:not(div:first)").css("color","red");
});
$("#btn4").click(function(){
$("div:even").css("color","red");
});
$("#btn5").click(function(){
$("div:odd").css("color","red");
});
$("#btn6").click(function(){
$("div:eq(2)").css("color","red");
});
$("#btn7").click(function(){
$("div:gt(2)").css("color","red");
});
$("#btn8").click(function(){
$("div:lt(2)").css("color","red");
});
$("#btn9").click(function(){
$("div:gt(1)").css("color","red");
});
})
</script>
</head>
<body>
<div>div1</div>
<div>div2</div>
<div>div3</div>
<div>div4</div>
<div>div5</div>
<div>div6</div>
<div>div7</div>
<div>div8</div>
<div>div9</div>
<div>div10</div>
<hr>
<input type="button" value="将第一个div中的文字改成红色" id="btn1">
<input type="button" value="将最后一个div中的文字改成红色" id="btn2">
<input type="button" value="将除第一个div的其他div中的文字改成红色" id="btn3">
<input type="button" value="将所有偶数位置的div改成红色" id="btn4">
<input type="button" value="将所有奇数位置的div改成红色" id="btn5">
<input type="button" value="将第3个div的文字改成红色" id="btn6">
<input type="button" value="将位置大于3的div文字改成红色" id="btn7">
<input type="button" value="将位置小于3的div文字改成红色" id="btn8">
<input type="button" value="将位置不小于3的div文字改成红色" id="btn9">
</body>
</html>
09_表单选择器.html
<!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="js/jquery-3.6.1.js"></script>
<script>
$(function () {
$("#btn1").click(function(){
$("input[type='text']:enabled").val("this is it")
});
$("#btn2").click(function(){
$("input[type='text']:disabled").val("this is it")
});
$("#btn3").click(function(){
var ck=$("input[type='checkbox']:checked").val()
alert(ck)
});
$("#btn4").on("click",function(){
var ct= $("option:selected").length
alert(ct)
});
})
</script>
</head>
<body>
<input type="text" name="不可用1" id="" disabled>
<input type="text" name="可用" id="">
<input type="text" name="不可用2" id="" disabled>
<input type="text" name="可用" id="">
<br>
<input type="button" value="使用val()改变可用的输入框中的名字" id="btn1">
<input type="button" value="使用val()改变不可用的输入框中的名字" id="btn2">
<br>
<hr>
<input type="checkbox" name="gender" id="" value="boy">男
<input type="checkbox" name="gender" id="" value="girl">女
<input type="button" value="获取选中的checkbox" id="btn3">
<br>
<hr>
<select multiple id="city">
<option value="cs">长沙</option>
<option value="bj">北京</option>
<option value="sh">上海</option>
</select>
<input type="button" value="显示选中的个数" id="btn4">
</body>
</html>
10_内容操作.html
<!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>内容操作</title>
<script src="js/jquery-3.6.1.js">
</script>
<style>
div {
border: 1px solid red;
}
#box1 {
width: 200px;
height: 200px;
}
#box2 {
width: 100px;
height: 100px;
}
#box3 {
width: 50px;
height: 50px;
}
</style>
<script>
$(function () {
$("#btn1").click(function(){
var ct= $("html").html()
alert(ct)
});
$("#btn2").click(function(){
$("div").html("Hello <b>world!</b>");
});
$("#btn3").click(function(){
var ct= $("div").text()
alert(ct)
});
$("#btn4").click(function(){
$("div").text("set text");
});
$("#btn5").click(function(){
var ct= $("input[type='text']").val()
alert(ct)
});
$("#btn6").click(function(){
$("input[type='text']").val("san");
});
})
</script>
</head>
<body>
<div id="box1">
<div id="box2">box2</div>
</div>
<br>
<input type="button" value="获取html内容" id="btn1">
<input type="button" value="设置html内容" id="btn2">
<input type="button" value="获取text内容" id="btn3">
<input type="button" value="设置text内容" id="btn4">
<br>
<hr>
<input type="text" name="username" id="username" value="王二麻子">
<input type="password" name="password" id="password" value="123456">
<input type="button" value="获取value属性值" id="btn5">
<input type="button" value="设置value属性值" id="btn6">
</body>
</html>