@[TPC]
功能: 控制html文档的内容
获取页面标签(元素)对象 Elemenmt
操作Element对象
<html lang="en">
<head>
<meta charset="UTF-8">
<title>获取元素对象title>
head>
<body>
<img id="light" src="img/off.gif">
<h1 id="title">悔创阿里杰克马h1>
<script>
//通过id获取元素对象
var light = document.getElementById("light");
alert("我要换图片了");
light.src="img/on.gif";
//获取h1标签镀锡
var title = document.getElementById("title");
alert("我要换内容了");
//修改内容
title.innerHTML="不知妻美刘强东";
script>
body>
html>
直接在html标签上,指定事件的属性(操作),属性值就是js代码
通过js获取元素对象,指定事件属性,设置一个函数
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件绑定title>
head>
<body>
<img id="light" src="img/off.gif" onclick="fun();">
<img id="light1" src="img/off.gif">
<script>
function fun() {
alert("我被点了");
alert("又被点了")
}
function fun1() {
alert("咋老点我")
}
//获取light1对象
var light1 = document.getElementById("light1");
//绑定事件
light1.onclick = fun1;
script>
body>
html>
分析:
1.获取图片对象
2.绑定单击事件
3.每次点击切换图片
* 规则:
* 如果灯是开的 on,切换图片为 off
* 如果灯是关的 off,切换图片为 on
* 使用标记flag来完成
<html lang="en">
<head>
<meta charset="UTF-8">
<title>电灯开关title>
head>
<body>
<img id="light" src="img/off.gif" >
<script>
//获取图片
var light = document.getElementById("light");
//灯的状态 关闭
var flag = false;
//绑定单机对象
light.onclick= function () {
if (flag){
light.src="img/off.gif";
flag =false;
} else{
//如果是关的,则打开
light.src="img/on.gif";
flag= true;
}
}
script>
body>
html>
概念:Browser Object Model 浏览器对象模型
组成:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>window对象title>
head>
<body>
<input id="openBtn" type="button" value="打开窗口">
<input id="closeBtn" type="button" value="关闭窗口">
<script>
//弹出方法
alert("hello window");
window.alert("hello a");
//确认框
var flag = confirm("您确定要退出吗?");
if (flag){
//退出操作
alert("欢迎再次光临!");
} else{
//提示
alert("手别抖");
}
//输入框
var result = prompt("请输入用户名");
alert(result);
//打开新窗口
var openBtn = document.getElementById("openBtn" );
var newWindow;
openBtn.onclick= function () {
//打开新窗口
newWindow = open("https://www.baidu.com");
}
//关闭新窗口
var closeBtn = document.getElementById("closeBtn");
closeBtn.onclick = function () {
//关闭新窗口
newWindow .close();
}
//定时器
//一次性的
var id = setTimeout(fun ,2000);
function fun() {
alert("boom");
}
//清除一次性定时器
clearTimeout(id)
//循环定时器
var id1=setInterval(fun ,2000);
//清除循环定时器
clearInterval(id1);
//获取history
var h1 = window.history;
var h2 = history;
alert(h1);
alert(h2);
var openBtn = window.document.getElementById("openBtn");
alert(openBtn);
/*document.getElementById("");与window.document.getElementById()相同*/
script>
body>
html>
分析:
1.在页面上使用img标签展示图片
2.定义一个方法,修改图片对象的src属性
3.定义一个定时器,每隔3秒调用方法一次。
<html lang="en">
<head>
<meta charset="UTF-8">
<title>轮播图title>
head>
<body>
<img id="img" src="img/banner_1.jpg">
<script>
//修改图片属性
var number =1;
function fun() {
number++;
//判断number是否大于3
if (number>3){
number =1;
}
//获取img对象
var img = document.getElementById("img");
img.src= "img/banner_"+number+".jpg";
}
//定义定时器//循环定时器
setInterval(fun,3000);
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Location对象title>
head>
<body>
<input type="button" id="btn" value="刷新">
<input type="button" id="gobaidu" value="去百度">
<script>
//获取按钮
//reload方法, 定义一个按钮,点击按钮刷新当前页面
var btn = document.getElementById("btn");
//绑定单击事件
btn.onclick = function () {
//刷新页面
location.reload();
}
//获取href
var href = location.href;
// 点击按钮,去访问www.baidu.com官网
var gobaidu = document.getElementById("gobaidu");
//绑定单击事件
gobaidu.onclick = function () {
//去访问
location.href = "https://www.baidu.com";
}
script>
body>
html>
分析:
1. 显示页面效果
2.倒计时读秒效果实现
2.1 定义一个方法,获取span标签,修改span标签体内容,时间–
2.2 定义一个定时器,1秒执行一次该方法
3.在方法中判断时间如果<= 0 ,则跳转到首页
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自动跳转到首页title>
<style>
span{
color: red;
}
p{
text-align: center;
}
style>
head>
<body>
<p>
<span id="time">5span>秒之后,跳转首页....
p>
<script>
var second = 5;
var time = document.getElementById("time");
//显示倒计时效果
function fun() {
second--;
if (second<=0){
//跳转
location.href = "https://www.baidu.com";
}
time.innerHTML = second +"";
}
//设置定时器
setInterval(fun ,1000);
script>
body>
html>
创建(获取):
1. window.history
2. history
方法:
属性:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>history对象title>
head>
<body>
<input type="button" id="btn" value="获取历史记录个数">
<a href="9_history2.html">9页面a>
<input type="button" id="forward" value="前进">
<input type="button" id="back" value="后退">
<script>
var btn= document.getElementById("btn");
btn.onclick = function () {
//获取当前窗口的历史记录
var length = history.length;
alert(length);
}
var forward = document.getElementById("forward");
forward.onclick=function () {
//前进
history.forward();
// history.go(1);
}
var back = document.getElementById("back");
back.onclick = function () {
history.back();
}
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态表格title>
<style>
table{
border: 1px solid;
margin: auto;
width: 500px;
}
td,th{
text-align: center;
border: 1px solid;
}
div{
text-align: center;
margin: 50px;
}
style>
head>
<body>
<div>
<input type="text" id="id" placeholder="请输入编号">
<input type="text" id="name" placeholder="请输入姓名">
<input type="text" id="gender" placeholder="请输入性别">
<input type="button" value="添加" id="btn_add">
div>
<table>
<caption>学生信息表caption>
<tr>
<th>编号th>
<th>姓名th>
<th>性别th>
<th>操作th>
tr>
<tr>
<td>1td>
<td>令狐冲td>
<td>男td>
<td><a href="javascript:void(0);" onclick="delTr(this);">删除a>td>
tr>
<tr>
<td>2td>
<td>任我行td>
<td>男td>
<td><a href="javascript:void(0);" onclick="delTr(this);">删除a>td>
tr>
<tr>
<td>3td>
<td>岳不群td>
<td>?td>
<td><a href="javascript:void(0);" onclick="delTr(this);" >删除a>td>
tr>
table>
<script>
//获取按钮
document.getElementById("btn_add").onclick = function () {
//获取文本框的内容
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
var gender = document.getElementById("gender").value;
//创建td . 赋值给td的标签
var td_id = document.createElement("td");
var text_id = document.createTextNode(id);
td_id.appendChild(text_id);
//name的id
var td_name = document.createElement("td")
var text_name = document.createTextNode(name);
td_name.appendChild(text_name);
//gender 的 td
var td_gender = document.createElement("td");
var text_gender = document.createTextNode(gender);
td_gender.appendChild(text_gender);
//a标签的id
var td_a = document.createElement("td");
var ele_a = document.createElement("a");
ele_a.setAttribute("href","javascript:void(0);");
ele_a.setAttribute("onclick","delTr(this);");
var text_a = document.createTextNode("删除");
ele_a.appendChild(text_a);
td_a.appendChild(ele_a);
//4.创建tr
var tr = document.createElement("tr");
//5.添加td到tr中
tr.appendChild(td_id);
tr.appendChild(td_name);
tr.appendChild(td_gender);
tr.appendChild(td_a);
//6.获取table
var table = document.getElementsByTagName("table")[0];
table.appendChild(tr);
}
//删除方法
function delTr(obj) {
var table = obj.parentNode.parentNode.parentNode;
var tr = obj.parentNode.parentNode;
table.removeChild(tr);
}
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表格全选title>
<style>
table{
border: 1px solid;
width: 500px;
margin-left: 30%;
}
td,th{
text-align: center;
border: 1px solid;
}
div{
margin-top: 10px;
margin-left: 30%;
}
.out{
background-color: white;
}
.over{
background-color: lightpink;
}
style>
<script>
//在页面加载完后绑定事件
window.onload = function () {
//全选按钮绑定单击事件
document.getElementById("selectAll").onclick = function () {
//全选
var cbs = document.getElementsByName("cb");
//遍历
for (var i = 0; i<cbs.length;i++){
//设置每一个cb的状态为选中
cbs[i].checked = true;
}
}
//全不选
document.getElementById("unSelectAll").onclick = function () {
//全不选
var cbs = document.getElementsByName("cb");
//遍历
for (var i = 0; i<cbs.length;i++){
//设置每一个cb的状态为未选中
cbs[i].checked = false;
}
}
//反选
document.getElementById("selectRev").onclick = function () {
//反选
var cbs = document.getElementsByName("cb");
//遍历
for (var i = 0; i<cbs.length;i++){
//设置每一个cb的状态为选中
cbs[i].checked = !cbs[i].checked;
}
}
document.getElementById("firstCb").onclick= function () {
//第一个cb点击
var cbs = document.getElementsByName("cb" );
for (var i = 0 ; i<cbs.length;i++){
cbs[i].checked = this.checked;
}
}
//给所有tr绑定鼠标移到元素之上和移出元素事件
var trs = document.getElementsByTagName("tr");
//2.遍历
for (var i = 0; i < trs.length; i++) {
//移到元素之上
trs[i].onmouseover = function(){
this.className = "over";
}
//移出元素
trs[i].onmouseout = function(){
this.className = "out";
}
}
}
script>
head>
<body>
<table>
<caption>学生信息表caption>
<tr>
<th><input type="checkbox" name="cb" id="firstCb">th>
<th>编号th>
<th>姓名th>
<th>性别th>
<th>操作th>
tr>
<tr>
<td><input type="checkbox" name="cb" >td>
<td>1td>
<td>令狐冲td>
<td>男td>
<td><a href="javascript:void(0);">删除a>td>
tr>
<tr>
<td><input type="checkbox" name="cb" >td>
<td>2td>
<td>任我行td>
<td>男td>
<td><a href="javascript:void(0);">删除a>td>
tr>
<tr>
<td><input type="checkbox" name="cb" >td>
<td>3td>
<td>岳不群td>
<td>?td>
<td><a href="javascript:void(0);">删除a>td>
tr>
table>
<div>
<input type="button" id="selectAll" value="全选">
<input type="button" id="unSelectAll" value="全不选">
<input type="button" id="selectRev" value="反选">
div>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单验证title>
<style>
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
body{
background: url("img/register_bg.png") no-repeat center;
padding-top: 25px;
}
.rg_layout{
width: 900px;
height: 500px;
border: 8px solid #EEEEEE;
background-color: white;
/*让div水平居中*/
margin: auto;
}
.rg_left{
/*border: 1px solid red;*/
float: left;
margin: 15px;
}
.rg_left > p:first-child{
color:#FFD026;
font-size: 20px;
}
.rg_left > p:last-child{
color:#A6A6A6;
font-size: 20px;
}
.rg_center{
float: left;
/* border: 1px solid red;*/
}
.rg_right{
/*border: 1px solid red;*/
float: right;
margin: 15px;
}
.rg_right > p:first-child{
font-size: 15px;
}
.rg_right p a {
color:pink;
}
.td_left{
width: 100px;
text-align: right;
height: 45px;
}
.td_right{
padding-left: 50px ;
}
#username,#password,#email,#name,#tel,#birthday,#checkcode{
width: 251px;
height: 32px;
border: 1px solid #A6A6A6 ;
/*设置边框圆角*/
border-radius: 5px;
padding-left: 10px;
}
#checkcode{
width: 110px;
}
#img_check{
height: 32px;
vertical-align: middle;
}
#btn_sub{
width: 150px;
height: 40px;
background-color: #FFD026;
border: 1px solid #FFD026 ;
}
.error{
color:red;
}
#td_sub{
padding-left: 150px;
}
style>
<script>
//加载完毕在运行
window.onload = function () {
//给表单绑定onsubmit
document.getElementById("form").onsubmit = function () {
//调用用户名检验方法 checkUsername();
//调用密码校验方法 chekPassword();
return checkUsername() && checkPassword();
}
//给用户名和密码框分别绑定离焦事件
document.getElementById("username").onblur = checkUsername;
document.getElementById("password").onblur = checkPassword;
}
//校验用户名
function checkUsername() {
//获取用户名的值
var username = document.getElementById("username").value;
//定义正则表达式
var reg_username =/^\w{6,12}$/;
//判断值是否符合正则
var flag = reg_username.test(username);
//提示信息
var s_username = document.getElementById("s_username");
if (flag){
//提示绿勾
s_username.innerHTML = "";
} else{
s_username.innerHTML = "用户名格式错误";
}
}
//校验密码
function checkPassword(){
//1.获取用户名的值
var password = document.getElementById("password").value;
//2.定义正则表达式
var reg_password = /^\w{6,12}$/;
//3.判断值是否符合正则的规则
var flag = reg_password.test(password);
//4.提示信息
var s_password = document.getElementById("s_password");
if(flag){
//提示绿色对勾
s_password.innerHTML = "";
}else{
//提示红色用户名有误
s_password.innerHTML = "密码格式有误";
}
return flag;
}
script>
head>
<body>
<div class="rg_layout">
<div class="rg_left">
<p>新用户注册p>
<p>USER REGISTERp>
div>
<div class="rg_center">
<div class="rg_form">
<form action="#" id="form" method="get">
<table>
<tr>
<td class="td_left"><label for="username">用户名label>td>
<td class="td_right">
<input type="text" name="username" id="username" placeholder="请输入用户名">
<span id="s_username" class="error">span>
td>
tr>
<tr>
<td class="td_left"><label for="password">密码label>td>
<td class="td_right">
<input type="password" name="password" id="password" placeholder="请输入密码">
<span id="s_password" class="error">span>
td>
tr>
<tr>
<td class="td_left"><label for="email">Emaillabel>td>
<td class="td_right"><input type="email" name="email" id="email" placeholder="请输入邮箱">td>
tr>
<tr>
<td class="td_left"><label for="name">姓名label>td>
<td class="td_right"><input type="text" name="name" id="name" placeholder="请输入姓名">td>
tr>
<tr>
<td class="td_left"><label for="tel">手机号label>td>
<td class="td_right"><input type="text" name="tel" id="tel" placeholder="请输入手机号">td>
tr>
<tr>
<td class="td_left"><label>性别label>td>
<td class="td_right">
<input type="radio" name="gender" value="male" checked> 男
<input type="radio" name="gender" value="female"> 女
td>
tr>
<tr>
<td class="td_left"><label for="birthday">出生日期label>td>
<td class="td_right"><input type="date" name="birthday" id="birthday" placeholder="请输入出生日期">td>
tr>
<tr>
<td class="td_left"><label for="checkcode" >验证码label>td>
<td class="td_right"><input type="text" name="checkcode" id="checkcode" placeholder="请输入验证码">
<img id="img_check" src="img/verify_code.jpg">
td>
tr>
<tr>
<td colspan="2" id="td_sub"><input type="submit" id="btn_sub" value="注册">td>
tr>
table>
form>
div>
div>
<div class="rg_right">
<p>已有账号?<a href="#">立即登录a>p>
div>
div>
body>
html>