总结一波第二天的练习题
1.给多个按钮注册点击事件,当点击按钮,显示对应的图片 -- title属性记录图片src
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button title="./images/1.jpg">按钮1</button>
<button title="./images/2.jpg">按钮2</button>
<button title="./images/3.jpg">按钮3</button>
<button title="./images/4.jpg">按钮4</button>
<button title="./images/5.jpg">按钮5</button>
<button title="./images/6.jpg">按钮6</button>
<br>
<img src="./images/1.jpg" alt="">
<script>
var im = document.querySelector('img')
var btns = document.querySelectorAll('button')
var index;
for(var i =0 ; i < btns.length ; i++){
btns[i].onclick = function(){
im.src = this.title
}
}
</script>
</body>
</html>
2.美女相册案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0 auto;
padding: 0;
list-style: none;
}
body{
margin-left: 100px;
}
ul{
overflow :hidden;
margin: 50px 0;
}
ul li {
float: left;
margin-right: 10px;
}
</style>
</head>
<body>
<h1>美女相册</h1>
<ul id="imgs">
<li>
<a href="image/1.jpg" title="美女1">
<img src="image/1-small.jpg" alt="">
</a>
</li>
<li>
<a href="image/2.jpg" title="美女2">
<img src="image/2-small.jpg" alt="">
</a>
</li>
<li>
<a href="image/3.jpg" title="美女3">
<img src="image/3-small.jpg" alt="">
</a>
</li>
<li>
<a href="image/4.jpg" title="美女4">
<img src="image/4-small.jpg" alt="">
</a>
</li>
</ul>
<img id="big" src="image/placeholder.png" alt="" width="400" height="250">
<p id="p">这是描述信息</p>
<script>
var as = document.querySelectorAll('a')
var big = document.querySelector('#big')
var p = document.querySelector('#p')
for(var i =0 ; i < as.length ; i++){
as[i].onclick = function(){
big.src = this.href
p.innerText = this.title
return false
}
}
</script>
</body>
</html>
3.二维码案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.small{
width: 50px;
height: 50px;
background:url(imag/bgs.png) no-repeat -159px -51px;
position: fixed;
top: 40%;
right: 10px;
}
.erweima{
position: absolute;
top: 0px;
left: -150px;
}
.small a {
display: block;
width: 50px;
height: 50px;
}
.hide {
display: none;
}
</style>
</head>
<body>
<div class="small">
<a href="#" id="link"></a>
<div class="erweima hide" id="er">
<img src="imag/456.png" alt="">
</div>
</div>
<script>
var link = document.querySelector('#link')
var er = document.querySelector('#er')
link.onmouseover = function(){
er.className = 'erweima'
}
link.onmouseout = function(){
er.className = 'hide'
}
</script>
</body>
</html>
4【案例:禁用多个文本框.html】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button>a按钮</button><br>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br>
<input type="text">
<script>
var btn = document.querySelector('button')
var inputs = document.querySelectorAll('input')
btn.onclick = function(){
for(var i =0 ; i < inputs.length ; i++){
inputs[i].disabled = true
}
this.disabled = true
}
</script>
</body>
</html>
6【案例:设置下拉框选中.html】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button id="btn">翻牌子</button>
<select id="sel">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<script>
var btn = document.querySelector('#btn')
var ops = document.querySelectorAll('#sel option')
btn.onclick = function(){
var i = parseInt(Math.random()*6)
console.log(i)
ops[i].selected = true
}
</script>
</body>
</html>
7【案例:仿京东搜索文本框案例.html】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" value="手机">
<script>
var ipt = document.querySelector('input')
ipt.onfocus = function(){
if(ipt.value == '手机'){
ipt.value = ""
}
}
ipt.onblur = function(){
if(ipt.value == ''){
ipt.value = "手机"
}
}
</script>
</body>
</html>
8【案例:表格全选案例.html】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 300px;
margin: 100px auto 0;
}
table{
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #c0c0c0;
width: 300px;
}
th,
td {
border: 1px solid #d0d0d0;
color: #404060;
padding: 10px;
}
th{
background-color: #09c;
color: #fff;
font: bold 16px "微软雅黑";
}
td{
font: bold 14px "微软雅黑";
}
tbody tr{
background-color: #fff;
text-align: center;
}
</style>
</head>
<body>
<div>
<table>
<thead>
<tr>
<th><input type="checkbox" id="all"></th>
<th>菜名</th>
<th>饭店</th>
</tr>
</thead>
<tbody id="dan">
<tr>
<td><input type="checkbox"></td>
<td>红烧肉</td>
<td>真好</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>红烧肉</td>
<td>真好</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>红烧肉</td>
<td>真好</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>红烧肉</td>
<td>真好</td>
</tr>
</tbody>
</table>
</div>
<script>
var all = document.querySelector('#all')
var chs = document.querySelectorAll('#dan input')
// 当点击全选时,全部一样
all.onclick = function(){
for(var i =0 ; i < chs.length ; i++){
chs[i].checked = all.checked
}
}
for(var i =0 ; i < chs.length ; i++){
chs[i].onclick = function(){
var n = 0
for(var i =0 ; i < chs.length ; i++){
if(chs[i].checked){
n++
}
}
if( n == 4){
all.checked = true
}else{
all.checked = false
}
}
}
</script>
</body>
</html>
9【案例:排他思想.html】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.red{
background-color: pink;
}
</style>
</head>
<body>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<button>按钮</button>
<script>
var btns = document.querySelectorAll('button')
for(var i =0 ; i < btns.length ; i++){
btns[i].onclick = function(){
for(var i =0 ; i < btns.length ; i++){
btns[i].className = ''
}
this.className = 'red'
}
}
</script>
</body>
</html>