JS常見案例總結(一)


@

1、登錄驗證

<!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>
        div {
            width: 600px;
            margin: 100px auto;
        }
        
        .message {
            display: inline-block;
            font-size: 12px;
            color: #999;
            background: url(images/mess.png) no-repeat left center;
            padding-left: 20px;
        }
        
        .wrong {
            color: red;
            background-image: url(images/wrong.png);
        }
        
        .right {
            color: green;
            background-image: url(images/right.png);
        }
    </style>
</head>

<body>
    <div class="register">
        <input type="password" class="ipt">
        <p class="message">請輸入6~16位密碼</p>
    </div>
    <script>
        // 首先判斷的事件是表單失去焦點 onblur
        // 如果輸入正確則提示正確的信息顏色為綠色小圖標變化
        // 如果輸入不是6到16位,則提示錯誤信息顏色為紅色 小圖標變化
        // 因為里面變化樣式較多,我們采取className修改樣式
        // 1.獲取元素
        var ipt = document.querySelector('.ipt');
        var message = document.querySelector('.message');
        //2. 注冊事件 失去焦點
        ipt.onblur = function() {
            // 根據表單里面值的長度 ipt.value.length
            if (this.value.length < 6 || this.value.length > 16) {
                // console.log('錯誤');
                message.className = 'message wrong';
                message.innerHTML = '您輸入的位數不對要求6~16位';
            } else {
                message.className = 'message right';
                message.innerHTML = '您輸入的正確';
            }
        }
    </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>
</head>

<body>
    <button>按鈕1</button>
    <button>按鈕2</button>
    <button>按鈕3</button>
    <button>按鈕4</button>
    <button>按鈕5</button>
    <script>
        // 1. 獲取所有按鈕元素
        var btns = document.getElementsByTagName('button');
        // btns得到的是偽數組  里面的每一個元素 btns[i]
        for (var i = 0; i < btns.length; i++) {
            btns[i].onclick = function() {
                // (1) 我們先把所有的按鈕背景顏色去掉  干掉所有人
                for (var i = 0; i < btns.length; i++) {
                    btns[i].style.backgroundColor = '';
                }
                // (2) 然后才讓當前的元素背景顏色為pink 留下我自己
                this.style.backgroundColor = 'pink';

            }
        }
        //2. 首先先排除其他人,然后才設置自己的樣式 這種排除其他人的思想我們成為排他思想
    </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>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            background: url(images/1.jpg) no-repeat center top;
        }
        
        li {
            list-style: none;
        }
        
        .baidu {
            overflow: hidden;
            margin: 100px auto;
            background-color: #fff;
            width: 410px;
            padding-top: 3px;
        }
        
        .baidu li {
            float: left;
            margin: 0 1px;
            cursor: pointer;
        }
        
        .baidu img {
            width: 100px;
        }
    </style>
</head>

<body>
    <ul class="baidu">
        <li><img src="images/1.jpg"></li>
        <li><img src="images/2.jpg"></li>
        <li><img src="images/3.jpg"></li>
        <li><img src="images/4.jpg"></li>
    </ul>
    <script>
        // 1. 獲取元素 
        var imgs = document.querySelector('.baidu').querySelectorAll('img');
        // console.log(imgs);
        // 2. 循環注冊事件 
        for (var i = 0; i < imgs.length; i++) {
            imgs[i].onclick = function() {
                // this.src 就是我們點擊圖片的路徑   images/2.jpg
                // console.log(this.src);
                // 把這個路徑 this.src 給body 就可以了
                document.body.style.backgroundImage = 'url(' + this.src + ')';
            }
        }
    </script>
</body>

</html>

在這里插入圖片描述

4、表格隔行換色

<!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>
        table {
            width: 800px;
            margin: 100px auto;
            text-align: center;
            border-collapse: collapse;
            font-size: 14px;
        }
        
        thead tr {
            height: 30px;
            background-color: skyblue;
        }
        
        tbody tr {
            height: 30px;
        }
        
        tbody td {
            border-bottom: 1px solid #d7d7d7;
            font-size: 12px;
            color: blue;
        }
        
        .bg {
            background-color: pink;
        }
    </style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>代碼</th>
                <th>名稱</th>
                <th>最新公布凈值</th>
                <th>累計凈值</th>
                <th>前單位凈值</th>
                <th>凈值增長率</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>003526</td>
                <td>農銀金穗3個月定期開放債券</td>
                <td>1.075</td>
                <td>1.079</td>
                <td>1.074</td>
                <td>+0.047%</td>
            </tr>
            <tr>
                <td>003526</td>
                <td>農銀金穗3個月定期開放債券</td>
                <td>1.075</td>
                <td>1.079</td>
                <td>1.074</td>
                <td>+0.047%</td>
            </tr>
            <tr>
                <td>003526</td>
                <td>農銀金穗3個月定期開放債券</td>
                <td>1.075</td>
                <td>1.079</td>
                <td>1.074</td>
                <td>+0.047%</td>
            </tr>
            <tr>
                <td>003526</td>
                <td>農銀金穗3個月定期開放債券</td>
                <td>1.075</td>
                <td>1.079</td>
                <td>1.074</td>
                <td>+0.047%</td>
            </tr>
            <tr>
                <td>003526</td>
                <td>農銀金穗3個月定期開放債券</td>
                <td>1.075</td>
                <td>1.079</td>
                <td>1.074</td>
                <td>+0.047%</td>
            </tr>
            <tr>
                <td>003526</td>
                <td>農銀金穗3個月定期開放債券</td>
                <td>1.075</td>
                <td>1.079</td>
                <td>1.074</td>
                <td>+0.047%</td>
            </tr>
        </tbody>
    </table>
    <script>
        // 1.獲取元素 獲取的是 tbody 里面所有的行
        var trs = document.querySelector('tbody').querySelectorAll('tr');
        // 2. 利用循環綁定注冊事件
        for (var i = 0; i < trs.length; i++) {
            // 3. 鼠標經過事件 onmouseover
            trs[i].onmouseover = function() {
                    // console.log(11);
                    this.className = 'bg';
                }
                // 4. 鼠標離開事件 onmouseout
            trs[i].onmouseout = function() {
                this.className = '';
            }
        }
    </script>
</body>

</html>

在這里插入圖片描述

5、全選與反選

<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .wrap {
            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;
            font: bold 16px "微軟雅黑";
            color: #fff;
        }
        
        td {
            font: 14px "微軟雅黑";
        }
        
        tbody tr {
            background-color: #f0f0f0;
        }
        
        tbody tr:hover {
            cursor: pointer;
            background-color: #fafafa;
        }
    </style>

</head>

<body>
    <div class="wrap">
        <table>
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" id="j_cbAll" />
                    </th>
                    <th>商品</th>
                    <th>價錢</th>
                </tr>
            </thead>
            <tbody id="j_tb">
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>iPhone8</td>
                    <td>8000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>iPad Pro</td>
                    <td>5000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>iPad Air</td>
                    <td>2000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>Apple Watch</td>
                    <td>2000</td>
                </tr>

            </tbody>
        </table>
    </div>
    <script>
        // 1. 全選和取消全選做法:  讓下面所有復選框的checked屬性(選中狀態) 跟隨 全選按鈕即可
        // 獲取元素
        var j_cbAll = document.getElementById('j_cbAll'); // 全選按鈕
        var j_tbs = document.getElementById('j_tb').getElementsByTagName('input'); // 下面所有的復選框
        // 注冊事件
        j_cbAll.onclick = function() {
                // this.checked 它可以得到當前復選框的選中狀態如果是true 就是選中,如果是false 就是未選中
                console.log(this.checked);
                for (var i = 0; i < j_tbs.length; i++) {
                    j_tbs[i].checked = this.checked;
                }
            }
            // 2. 下面復選框需要全部選中, 上面全選才能選中做法: 給下面所有復選框綁定點擊事件,每次點擊,都要循環查看下面所有的復選框是否有沒選中的,如果有一個沒選中的, 上面全選就不選中。
        for (var i = 0; i < j_tbs.length; i++) {
            j_tbs[i].onclick = function() {
                // flag 控制全選按鈕是否選中
                var flag = true;
                // 每次點擊下面的復選框都要循環檢查者4個小按鈕是否全被選中
                for (var i = 0; i < j_tbs.length; i++) {
                    if (!j_tbs[i].checked) {
                        flag = false;
                     // 退出for循環 這樣可以提高執行效率 因為只要有一個沒有選中,剩下的就無需循環判斷了
                    }
                }
                j_cbAll.checked = flag;
            }
        }
    </script>
</body>
</html>

在這里插入圖片描述

6、tab欄切換

<!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;
        }
        
        li {
            list-style-type: none;
        }
        
        .tab {
            width: 978px;
            margin: 100px auto;
        }
        
        .tab_list {
            height: 39px;
            border: 1px solid #ccc;
            background-color: #f1f1f1;
        }
        
        .tab_list li {
            float: left;
            height: 39px;
            line-height: 39px;
            padding: 0 20px;
            text-align: center;
            cursor: pointer;
        }
        
        .tab_list .current {
            background-color: #c81623;
            color: #fff;
        }
        
        .item_info {
            padding: 20px 0 0 20px;
        }
        
        .item {
            display: none;
            height: 300px;
            background-color: #c81623;
        }
        .item:nth-child(2){
            background-color: aqua;
            
        }
        .item:nth-child(3){
            background-color: #393953;
        }
        .item:nth-child(4){
            background-color: #898989;
        }
        .item:nth-child(5){
            background-color: #ff78ff; 
        }
    </style>
</head>

<body>
    <div class="tab">
        <div class="tab_list">
            <ul>
                <li class="current">商品介紹</li>
                <li>規格與包裝</li>
                <li>售后保障</li>
                <li>商品評價(50000)</li>
                <li>手機社區</li>
            </ul>
        </div>
        <div class="tab_con">
            <div class="item" style="display: block;">
                商品介紹模塊內容
            </div>
            <div class="item">
                規格與包裝模塊內容
            </div>
            <div class="item">
                售后保障模塊內容
            </div>
            <div class="item">
                商品評價(50000)模塊內容
            </div>
            <div class="item">
                手機社區模塊內容
            </div>

        </div>
    </div>
    <script>
        // 獲取元素
        var tab_list = document.querySelector('.tab_list');
        var lis = tab_list.querySelectorAll('li');
        var items = document.querySelectorAll('.item');
        // for循環綁定點擊事件
        for (var i = 0; i < lis.length; i++) {
            // 開始給5個小li 設置索引號 
            lis[i].setAttribute('index', i);
            lis[i].onclick = function() {
                // 1. 上的模塊選項卡,點擊某一個,當前這一個底色會是紅色,其余不變(排他思想) 修改類名的方式

                // 干掉所有人 其余的li清除 class 這個類
                for (var i = 0; i < lis.length; i++) {
                    lis[i].className = '';
                }
                // 留下我自己 
                this.className = 'current';
                // 2. 下面的顯示內容模塊
                var index = this.getAttribute('index');
                console.log(index);
                // 干掉所有人 讓其余的item 這些div 隱藏
                for (var i = 0; i < items.length; i++) {
                    items[i].style.display = 'none';
                }
                // 留下我自己 讓對應的item 顯示出來
                items[index].style.display = 'block';
            }
        }
    </script>
</body>

</html>

在這里插入圖片描述

7、簡單發布留言

<!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;
        }
        
        body {
            padding: 100px;
        }
        
        textarea {
            width: 200px;
            height: 100px;
            border: 1px solid pink;
            outline: none;
            resize: none;
        }
        
        ul {
            margin-top: 50px;
        }
        
        li {
            width: 300px;
            padding: 5px;
            background-color: rgb(245, 209, 243);
            color: red;
            font-size: 14px;
            margin: 15px 0;
        }
    </style>
</head>

<body>
    <textarea name="" id=""></textarea>
    <button>發布</button>
    <ul>

    </ul>
    <script>
        // 1. 獲取元素
        var btn = document.querySelector('button');
        var text = document.querySelector('textarea');
        var ul = document.querySelector('ul');
        // 2. 注冊事件
        btn.onclick = function() {
            if (text.value == '') {
                alert('您沒有輸入內容');
                return false;
            } else {
                // console.log(text.value);
                // (1) 創建元素
                var li = document.createElement('li');
                // 先有li 才能賦值
                li.innerHTML = text.value;
                // (2) 添加元素
                // ul.appendChild(li);
                ul.insertBefore(li, ul.children[0]);
            }
        }
    </script>
</body>

</html>

在這里插入圖片描述

8、三級聯動

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		form{
			width: 500px;
			height: 200px;
			background-color: aquamarine;
			margin: 30px auto;
			padding: 100px 85px;
			box-sizing: border-box;
			position: relative;
		}
		form select{
			width: 100px;
			height: 30px;
		}
		form .title{
			text-align: center;
			position: absolute;
			top: 25px;
			left: 50%;
			transform: translateX(-50%);
		}
	</style>
</head>
<body>
	<form action="">
		<div class="title">省市區 三級聯動</div>
		<select  id="sheng">
			<option value="-1">請選擇</option>
		</select>
		<select  id="shi"></select>
		<select  id="xian"></select>
	</form>
	<script>
		var shengs = ['山東省','江蘇省','河北省'];

		var shis = [
			['濟南市','青島市','煙台市','臨沂市'],
			['南京市','徐州市','無錫市','常州市'],
			['石家庄市','唐山市','秦皇島市','張家口市']
		];

		var xianqus =[
			[
			 ['歷下區','市中區','槐蔭區','長清區'],
			 ['市南區','市北區','四方區','黃島區'],
			 ['芝罘區','萊山區','牟平區','蓬萊市'],
			 ['蘭山區','北城新區','河東區','羅庄區']
			 ],[
			 ['玄武區','白下區','秦淮區','鼓樓區'],
			 ['雲龍區','九里區','賈汪區','泉山區'],
			 ['崇安區','南長區','北塘區','錫山市'],
			 ['天寧區','鍾樓區','新北區','武進區']
			 ],[
			 ['長安區','橋東區','橋西區','新華區'],
			 ['開平區','古治區','路北區','路南區'],
			 ['海港區','山海關區','北戴河區','盧龍縣'],
			 ['宣化區','下花園區','張北縣','沽源區']	 
			 ]
			];
			
			var sheng =document.getElementById('sheng');
			var shi = document.getElementById('shi');
			var xianqu = document.getElementById('xian');
			function adds(bq,data){
				for(var i in data){
					var datas = new Option(data[i],i);
					bq.options.add(datas);
				}
			}

			adds(sheng,shengs);
			sheng.onchange = function(){
				shi.options.length = 0;
				adds(shi,shis[sheng.value]);
				if(sheng.value >= 0){
					shi.onchange();
				}else{
					xianqu.options.length = 0;
				}
			}

			shi.onchange = function(){
				xianqu.options.length = 0;
				adds(xianqu,xianqus[sheng.value][shi.value]);
			}
	</script>
</body>
</html>

在這里插入圖片描述

9、時鍾

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<!-- css -->
	<style>
	
	*{margin: 0;padding: 0;list-style: none;
	}
	
	#box{width: 600px;height: 600px;background: url(https://gitee.com/miaoxiaocunzai/blog/raw/master/clock.jpg) no-repeat;
		margin: 10px auto;

		position: relative;
	}

	#hour,#min,#second{width: 30px;height: 600px;position: absolute;left: 50%;top:10px;
		margin-left: -15px;
	}
	
	#hour{background: url(./images/hour.png) no-repeat center center;}
	#min{background: url(./images/minute.png) no-repeat center center;}
	#second{background: url(./images/second.png) no-repeat center center;}
	</style>
</head>
<body>
	<!-- html -->
	<div id="box">
		<div id="hour"></div>
		<div id="min"></div>
		<div id="second"></div>
	</div>
</body>
<!-- JavaScript -->
<script>
	window.onload =function(){
		// 獲取需要的標簽
		var hour = document.getElementById("hour");
		var min = document.getElementById("min");
		var second = document.getElementById("second");
		// 開始定時器
		setInterval(function(){
		// 獲取當前的時間
		var date = new Date();
		// 求出總毫秒數
		var millS =date.getMilliseconds();
		// 秒
		var s = date.getSeconds() + millS/1000;
		// 分
		var m =	date.getMinutes() + s/60;
		// 時
		var h = date.getHours() % 12 + m/60;
		// 旋轉起來                          
		hour.style.transform = 'rotate('+ h * 30 +'deg)';
        min.style.transform = 'rotate('+ m * 6 +'deg)';
		second.style.transform = 'rotate('+ s * 6 +'deg)';
	},10);
}
	</script>
</html> 

在這里插入圖片描述

10、輪播圖

   <!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>輪播圖的實現</title>
    <style>
	    	* {
	    box-sizing: border-box;
	    padding: 0;
	    margin: 0;
	}
	
	body {
	    background-color: gray;
	}
	
	div {
	    width: 700px;
	    height: 400px;
	    margin: 100px auto;
	    position: relative;
	}
	
	ul {
	    list-style: none;
	}
	
	.images li {
	    position: absolute;
	    opacity: 0;
	    transition: 0.6s;
	}
	
	.images .active {
	    opacity: 1;
	}
	
	.images img {
	    width: 700px;
	    height: 400px;
	    cursor: pointer;
	}
	
	.dots {
	    position: absolute;
	    bottom: 20px;
	    left: 250px;
	}
	
	.dots li {
	    width: 15px;
	    height: 15px;
	    border-radius: 50%;
	    border: solid 2px #fff;
	    display: inline-block;
	    margin-right: 10px;
	    cursor: pointer;
	    transition: 0.6s;
	}
	
	.dots .white {
	    background-color: #fff;
	    box-shadow: 0 0 1px 5px rgba(0, 0, 0, 0.3)
	}
	.fa{
	    color: #fff;
	    cursor: pointer;
	    /* background-color:rgb(29, 26, 26); */
	    width: 40px;
	    height: 60px;
	    text-align: center;
	    line-height: 60px;
	    opacity: 0.3;
	}
	.fa:hover{
	    opacity: 0.6;
	}
	#left{
	    position: absolute;
	    top: 180px;
	}
	#right{
	    position: absolute;
	    top: 180px;
	    right: 0;
	}
    </style>
</head>
<body>
    <div class="banner">
        <ul class="images">
            <li class="active">
                <img src="imgs/57523a17c02ad.jpg" alt="">
            </li>
            <li>
                <img src="imgs/59c898949b98b.jpg" alt="">
            </li>
            <li>
                <img src="imgs/5bbd6379b2b20.jpg" alt="">
            </li>
            <li>
                <img src="imgs/81a9a1506c024141ea5ea8888e935ca3bbd7bf56.jpg" alt="">
            </li>
            <li>
                <img src="imgs/one.jpg" alt="">
            </li>
            <li>
                <img src="imgs/vkleVabkBxvBupx.jpg" alt="">
            </li>
        </ul>
        <ul class="dots">
            <li class="white"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <i id="left" class="fa fa-chevron-left fa-3x"></i>
        <i id="right" class="fa fa-chevron-right fa-3x"></i>
    </div>
</body>
<script >
var lisImg = document.querySelectorAll(".images li");
var lisDot = document.querySelectorAll(".dots li");
var banner = document.querySelector("div");

var left = document.getElementById("left");
var right = document.getElementById("right");

var timer;
var number = 0;

function show() {
    for (var j = 0; j < lisDot.length; j++) {
        lisDot[j].className = "";
        lisImg[j].className = "";
    }
    lisDot[number].className = "white";
    lisImg[number].className = "active";
}
show()

// 往下輪播下一張
var next = function () {
    number++;
    if (number >= lisDot.length) {
        number = 0;
    }
    show();
}
timer = setInterval(next, 1500);

// 往上輪播上一張
// function prev(){
//     timer = setInterval(function(){
//         number--;
//         if(number < 0){
//             number = lisDot.length - 1;
//         }
//         show();
//     }, 1000);
// }
// prev()

// 當鼠標放在圖片上時,停止自動輪播,清除定時器
banner.onmousemove = function () {
    clearInterval(timer);
}
// 當鼠標離開圖片時,繼續進行自動輪播
banner.onmouseleave = function () {
    timer = setInterval(next, 1500);
}

// 滑點輪播
for (var i = 0; i < lisDot.length; i++) {
    lisDot[i].index = i;
    lisDot[i].onmousemove = function () {
        // clearInterval(timer);
        for (var k = 0; k < lisDot.length; k++) {
            lisDot[k].className = "";
            lisImg[k].className = "";
        }
        this.className = "white";
        lisImg[this.index].className = "active";
    }
  
    lisDot[i].onmouseleave = function () {
        number = this.index;
        timer = setInterval(next, 1500);
    }
}

left.onclick = function () {
    number--;
    if (number < 0) {
        number = lisDot.length - 1;
    }
    show();
}

// 下一張
right.onclick = function () {
    number++;
    if (number >= lisDot.length) {
        number = 0;
    }
    show();
}

</script>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM