第八节课
表单-启用&禁用文本框
<!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>
</head>
<body>
<input type="text" value="admin" placeholder="输入账号">
<input type="password" value="123123">
<button>禁用</button>
<button>启用</button>
<script>
let btnList = document.getElementsByTagName('button')
let input = document.getElementsByTagName('input')[0]
let btn1 = btnList[0]
let btn2 = btnList[1]
btn1.onclick = function() {
input.disabled = true
}
btn2.onclick = function() {
input.disabled = false
}
</script>
</body>
</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>
lable {
display: block;
}
.show {
display: block;
}
.hide {
display: none;
}
</style>
</head>
<body>
<!-- <label for="input1">
京东: <input type="text" id="input1" value="相机">
</label>-->
<label for="input2">
淘宝:
</label>
<input type="text" id="input2" value="电脑">
<!--placehoolder:
<input type="text" placeholder="placeholder">-->
<script>
let input1 = document.getElementById('input1')
let input2 = document.getElementById('input2')
let lab = document.getElementsByTagName('label')[0]
input2.oninput = function () {
if(this.value === '') {
lab.className = 'show'
}else{
lab.className = 'hide'
}
}
input1.onfocus = function() {
this.value = ''
this.style.color = '#f00'
}
input1.onblur = function() {
this.value = '相机'
this.style.color = '#000'
}
</script>
</body>
</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>
.right {
border: 2px solid rgb(50, 158, 50);
}
.wrong {
border: 2px solid rgb(211, 49, 49);
}
</style>
</head>
<body>
账号:<input type="text" onblur="fn(this)">
密码:<input type="password" onblur="fn(this)">
<script>
function fn(a) {
if(a.value.length <12 && a.value.length>8){
a.className = 'right'
} else {
a.className = 'wrong'
}
}
</script>
</body>
</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>
* {
margin: 0;
padding: 0;
}
.my-table {
width: 500px;
margin: 100px auto;
}
table {
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #ccc;
width: 500px;
}
th,
td {
border: 1px solid #ccc;
color: rgb(90, 90, 167);
padding: 10px;
}
th {
font:bold 16px "微软雅黑";
background-color:#09c;
color: #fff;
}
td {
font:bold 14px "微软雅黑";
}
tbody tr {
background-color: #f6f6f6;
}
tbody tr:hover {
cursor: pointer;
background-color: #09c;
}
</style>
</head>
<body>
<div class="my-table">
<table>
<thead>
<tr>
<th>
<input type="checkbox" id="all">
</th>
<th>菜名</th>
<th>饭店</th>
</tr>
</thead>
<tbody id ="content">
<tr>
<td>
<input type="checkbox">
</td>
<td>菜品1</td>
<td>面包店</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>菜品2</td>
<td>烧烤店</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>菜品3</td>
<td>奶茶店</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>菜品4</td>
<td>火锅店</td>
</tr>
</tbody>
</table>
</div>
<script>
window.onload=function() {
let allBtn = document.getElementById('all')
let tBody = document.getElementById('content')
let inputList = tBody.getElementsByTagName('input')
allBtn.onclick = function() {
for(let index = 0;index < inputList.length;index++) {
inputList[index].checked = this.checked
}
}
for(let index = 0;index < inputList.length;index++) {
inputList[index].onclick = function() {
let flag = true
for(let i = 0;i < inputList.length;i++) {
if (inputList[i].checked === false) {
flag = false
}
}
allBtn.checked = flag
}
}
}
</script>
</body>
</html>
表单-Tab切换(排他性)
<!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>Tab切换(排他性)</title>
<style>
button {
margin: 10px;
width: 100px;
height: 40px;
cursor: pointer;
}
.current {
background-color: #f00;
}
</style>>
</head>
<body>
<button>按钮1</button>
<button>按钮2</button>
<button>按钮3</button>
<button>按钮4</button>
<button>按钮5</button>
<script>
let btnList = document.getElementsByTagName('button')
for(let i = 0;i < btnList.length;i++) {
btnList[i].onmouseover = function() {
for(let j = 0;j < btnList.length;j++) {
btnList[j].className=''
}
this.className='current'
}
}
for(let i = 0;i < btnList.length;i++) {
btnList[i].onmouseout = function() {
this.className=''
}
}
</script>
</body>
</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>
</head>
<body>
<!--下面这个ID是img的element是略缩图,鼠标悬停在它上面即出现原图-->
<!--onmouseover方法中调用了mouseOverPic函数,传给它的参数是原图的URL-->
<!--调用mouseOverPic之后,将原图显示区域设为可见-->
<img id="img" src="me.jpg" width="50px" height="50px" onmouseover="mouseOverPic('me.jpg'); bigPic.style.visibility='visible'" onmouseout="mouseOutPic()">
<!--页面初始化的时候,原图显示区域是隐藏的-->
<div id="bigPic" style="visibility:hidden;">
</div>
<script language=JavaScript>
function mouseOutPic() //当鼠标移出时,隐藏原图
{
if(window.event.toElement.id!="img") bigPic.style.visibility = "hidden";
//如果鼠标不在img000上的话,将这个区域隐藏
}
function mouseOverPic(imgSrc) //当鼠标移上小图时,显示原图
{
var seeBig = document.getElementById("bigPic"); //得到将要显示原图区域的div element
var newImg = document.createElement("img"); //新建一个img element,将原图的src赋给这个element
newImg = "<img src=\'" + imgSrc + "\'/>"; //注意这里,是为整个element赋值,我在这个小问题上还花了挺多时间
//alert(imgSrc);
seeBig.innerHTML = newImg; //将原图赋给要显示的区域
}
</script>
</body>
</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>
ul {
padding: 0;
}
li {
float: left;
padding: 1em;
list-style: none;
}
li a img {
border: 0;
}
.clear {
clear: both;
}
#image-gallery {
width: 500px;
overflow: auto;
display: flex;
background-color: silver;
}
#mask {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: rgba(0, 0, 0, 0.5);
display: none;
}
.preview {
width: 800px;
height: 500px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -400px;
margin-top: -250px;
}
#image-gallery {
widows: 500px;
overflow: auto;
display: flex;
background-color: #b292ac;
}
#mask {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: rgba(0, 0, 0, 0.6);
display: none;
}
</style>
</head>
<body>
<h2>图片画廊</h2>
<ul id = "image-gallery">
<li>
<a href="img/1.jpg" title="图片1">
<img src="1.jpg" alt="图片1" width="100">
</a>
</li>
<li>
<a href="img/2.jpg" title="图片2">
<img src="2.jpg" alt="图片2" width="100">
</a>
</li>
<li>
<a href="img/3.jpg" title="图片3">
<img src="3.jpg" alt="图片3" width="100">
</a>
</li>
<li>
<a href="img/4.jpg" title="图片4">
<img src="4.jpg" alt="图片4" width="100">
</a>
</li>
<li>
<a href="img/5.jpg" title="图片5">
<img src="5.jpg" alt="图片5" width="100">
</a>
</li>
</ul>
<div class="clear"></div>
<!--<img id = "image" src="150.png" />-->
<p id="des">选择一个图片</p>
<div id="mask">
<img id="image" class="preview" />
</div>
<script>
let ul = document.getElementById('image-gallery')
let arr = ul.getElementsByTagName('a')
let img = document.getElementById('image')
let des = document.getElementById('des')
let mask = document.getElementById('mask')
for (let index = 0;index < arr.length;index++) {
const element = arr[index];
element.onclick = function() {
ul.style.zIndex = -999
mask.style.display = 'block'
img.src = this.href
return false
}
img.onclick = function() {
mask.style.display='none'
ul.style.zIndex = 999
}
}
</script>
</body>
</html>