dom操作演示、选择器操作演示、鼠标事件演示
作业标题:0721作业
作业内容:
1、dom
操作演示
2、选择器操作演示
3、鼠标事件演示
- dom操作演示
<!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="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
<ul class="list">
<li class="item">item1</li>
<li class="item">item2</li>
<li class="item">item3</li>
<li class="item">item4</li>
<li class="item">item5</li>
<li class="item">item6</li>
<li class="item">item7</li>
<li class="item">item8</li>
<li class="item">item9</li>
<li class="item">item10</li>
</ul>
<p>这是p标签</p>
<p>这是p标签</p>
<button>按钮</button>
</body>
<script>
//dom元素:添加、更新、删除
//1、append()
//选择父元素、在它里面追加一个元素,元素结尾追加内容
// $("button").click(function () {
// $(".list").append('<li class="item">item11</li>');
// });
//2、prepend元素的开头插入指定内容
//$(".list").prepend('<li class="item">item0</li>');
//3、after()元素后插入指定的内容
// $(".list").after("<li style='color:red'>item11</li>");
// //4、before()元素之前插入指定内容
// $(".list").before("<li>item12</li>");
//5.replaceWith()用新的内容替换匹配的元素
$("button").click(function () {
//$("p").replaceWith("<li class='item'>item11</li>");
//6.replaceAll()用匹配的元素替换所有匹配的元素
$("<li class='item'>item11</li>").replaceAll("p");
//11、remove()删除元素,包括文本和子节点
$("p").remove();
});
</script>
</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="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<style>
* {
text-align: center;
font-size: 20px;
}
.title {
text-align: center;
}
.width {
width: 1200px;
}
tr {
height: 50px;
}
button {
width: 600px;
color: #fff;
background-color: #28a745;
border-color: #28a745;
margin-top: 30px;
}
</style>
</head>
<body>
<h3 class="title" id="titleId">php中文网名单</h3>
<table class="width" id="tableId" border="1" align="center" cellspacing="0">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>邮箱</th>
<th>电话</th>
<th>省份</th>
<th>城市</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr>
<th>1</th>
<td>天蓬</td>
<td>tianpeng@php.cn</td>
<td>13854381111</td>
<td>安徽</td>
<td>合肥</td>
<td>40</td>
</tr>
<tr>
<th>2</th>
<td>欧阳克</td>
<td>ouyangke@php.cn</td>
<td>13854382222</td>
<td>安徽</td>
<td>马鞍山</td>
<td>40</td>
</tr>
<tr>
<th>3</th>
<td>灭绝师太</td>
<td>miejue@php.cn</td>
<td>13854383333</td>
<td>安徽</td>
<td>滁州</td>
<td>18</td>
</tr>
<tr>
<th>4</th>
<td>裘千丈</td>
<td>qiuqianzhang@php.cn</td>
<td>13854384444</td>
<td>安徽</td>
<td>蚌埠</td>
<td>40</td>
</tr>
<tr>
<th>5</th>
<td>钟老师</td>
<td>zhonglaoshi@php.cn</td>
<td>13854385555</td>
<td>安徽</td>
<td>淮南</td>
<td>30</td>
</tr>
<tr>
<th>6</th>
<td>小编1</td>
<td>xiaobian1@php.cn</td>
<td>13854386661</td>
<td>安徽</td>
<td>安庆</td>
<td>18</td>
</tr>
<tr>
<th>7</th>
<td>小编2</td>
<td>xiaobian2@php.cn</td>
<td>13854386662</td>
<td>安徽</td>
<td>亳州</td>
<td>18</td>
</tr>
<tr>
<th>8</th>
<td>小编3</td>
<td>xiaobian3@php.cn</td>
<td>13854386663</td>
<td>安徽</td>
<td>淮北</td>
<td>18</td>
</tr>
<tr>
<th>9</th>
<td>小编4</td>
<td>xiaobian4@php.cn</td>
<td>13854386664</td>
<td>安徽</td>
<td>阜阳</td>
<td>18</td>
</tr>
<tr>
<th>10</th>
<td>小编5</td>
<td>xiaobian5@php.cn</td>
<td>13854386665</td>
<td>安徽</td>
<td>六安</td>
<td>18</td>
</tr>
</tbody>
</table>
<ul class="list">
<li class="item">item1</li>
<li class="item">item2</li>
<li class="item">item3</li>
<li class="item">item4</li>
<li class="item">item5</li>
<li class="item">item6</li>
<li class="item">item7</li>
<li class="item">item8</li>
<li class="item">item9</li>
<li class="item">item10</li>
</ul>
<!-- <button id="up">上一页</button> -->
<button id="per">下一页</button>
</body>
<script>
$("button").click(function () {
//选择器
//节点选择,但是不能单独中其中1条
//1、:first选择第一个元素
// $("tbody tr:first").css("color", "lightpink");
//2、:last选择最后一个元素
// $("tr:last").css("color", "red");
// //3、:even选择偶数,从0开始,0,2,4,6,8,10
// $("tr:even").css("color", "red");
// //4、:odd选择奇数,从1开始
// $("tr:odd").css("color", "lime");
//5、:first-child选择你元素的第一个元素
// $("li:first-child").css("color", "red");
//6、first-of-type 选择父元素的第一个元素
// $("li:first-of-type").css("background-color", "yellow");
//7、:last-child选择父元素里的最后一个元素
//8、:last-of-type选择父元素里的最后一个元素
$("li:last-child").css("color", "red");
$("li:last-of-type").css("background-color", "blue");
});
</script>
</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="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<style>
* {
background-color: #d4edda;
text-align: center;
font-size: 20px;
}
.form-control {
width: 500px;
padding: 0.375rem 0.75rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: 0.25rem;
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}
button {
width: 600px;
color: #fff;
background-color: #28a745;
border-color: #28a745;
}
.select {
width: 265px;
height: 47px;
}
</style>
</head>
<body>
<h2 class="title">注册</h2>
<form action="" onsubmit="return false;">
<p>
账户:
<input type="text" class="form-control" id="account" />
</p>
<p>
密码:
<input type="password" class="form-control" id="password" />
</p>
<p>
省市:
<select id="prov" class="form-control select">
<option value="">请选择</option>
<option value="1">安徽</option>
<option value="2">江苏</option>
<option value="3">河南</option>
</select>
<select id="city" class="form-control select">
<option value="">请选择</option>
</select>
</p>
<button>注册</button>
</form>
</body>
<script>
//1、click点击事件
// $("button").click(function () {
// let account = $("#account").val();
// if (!account) {
// alert("请输入账户");
// return false;
// }
// alert("成功");
// });
// 2、必须快速,连续点击2次,这个事件才触发dblclick()双击事件
// $("button").dblclick(function () {
// alert("双击事件");
// });
//3、mouseenter()鼠标放到元素上,就触发
// $("#account").mouseenter(function () {
// let account = $("#account").val();
// if (!account) {
// alert("请输入账户");
// return false;
// }
// alert("成功");
// });
//4、mouseleave()鼠标移开元素,就触发
// $("#account").mouseleave(function () {
// alert("鼠标移开触发");
// });
//5、hover()鼠标触碰和移开,都会触发
// $("button").hover(
// function () {
// console.log("鼠标放到按钮上了");
// },
// function () {
// console.log("鼠标从按钮上移开了");
// }
// );
$("button").click(function () {
let account = $("#account").val();
if (!account) {
alert("请输入账户");
return false;
}
if (account == "admin" || account == "php中文网") {
alert("该账户不能使用");
return false;
}
let password = "#password".val();
if (!password) {
alert("请输入密码");
return false;
}
if (password.length <= 6) {
alert("请输入大于6位的密码");
return false;
}
alert("成功"); //代表把数据提交给数据库了
});
</script>
</html>