<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        使用JS實現(xiàn)導(dǎo)航切換時高亮顯示的示例講解

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:09:31
        文檔

        使用JS實現(xiàn)導(dǎo)航切換時高亮顯示的示例講解

        使用JS實現(xiàn)導(dǎo)航切換時高亮顯示的示例講解:index.html代碼內(nèi)容 <!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>導(dǎo)航高亮顯示</title> <style type=text/css> body{ font-size:20
        推薦度:
        導(dǎo)讀使用JS實現(xiàn)導(dǎo)航切換時高亮顯示的示例講解:index.html代碼內(nèi)容 <!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>導(dǎo)航高亮顯示</title> <style type=text/css> body{ font-size:20

        index.html代碼內(nèi)容

        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        	<title>導(dǎo)航高亮顯示</title>
        	<style type="text/css">
        	body{
        	font-size:20px;
        	}
        	.nav{
        	list-style-type: none;
        	margin:0;
        	padding:0;
        	}
        	.clear:after{
        	content:'/20';
        	display:block;
        	clear:both;
        	height:0;
        	visibility: hidden;
        	}
        	.nav li{
        	float:left;
        	background:#B1DFF5;
        	margin-right:1px;
        	color:#fff;
        	}
        	.nav li a{
        	display:block;
        	height:45px;
        	width:120px;
        	line-height:45px;
        	text-align:center;
        	text-decoration:none;
        	}
        	.active{
        	background:#28b1f3;
        	font-weight:bold;
        	}
        	</style>
        </head>
        <body>
        	<ul class="nav clear" id="nav">
        	<li><a href="index.html" rel="external nofollow" rel="external nofollow" >首頁</a></li>
        	<li><a href="1.html" rel="external nofollow" rel="external nofollow" >欄目一</a></li>
        	<li><a href="2.html" rel="external nofollow" rel="external nofollow" >欄目二</a></li>
        	<li><a href="3.html" rel="external nofollow" rel="external nofollow" >欄目三</a></li>
        	</ul>
        </body>
        <script type="text/javascript" src="js/jquery-1.7.min.js"></script>
        <script type="text/javascript">
        	var urlstr = location.href;
        	console.log(urlstr+'/');
         var urlstatus=false;
         $("#nav a").each(function () { 
         if ((urlstr + '/').indexOf($(this).attr('href')) > -1&&$(this).attr('href')!='') {
         $(this).addClass('active'); urlstatus = true;
         } else {
         $(this).removeClass('active');
         }
         });
         if (!urlstatus) {$("#nav a").eq(0).addClass('active'); }
        </script>
        </html>
        

        1.html代碼內(nèi)容

        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        	<title>欄目一</title>
        	<style type="text/css">
        	body{
        	font-size:20px;
        	}
        	.nav{
        	list-style-type: none;
        	margin:0;
        	padding:0;
        	}
        	.clear:after{
        	content:'/20';
        	display:block;
        	clear:both;
        	height:0;
        	visibility: hidden;
        	}
        	.nav li{
        	float:left;
        	background:#B1DFF5;
        	margin-right:1px;
        	color:#fff;
        	}
        	.nav li a{
        	display:block;
        	height:45px;
        	width:120px;
        	line-height:45px;
        	text-align:center;
        	text-decoration:none;
        	}
        	.active{
        	background:#28b1f3;
        	font-weight:bold;
        	}
        	</style>
        </head>
        <body>
        <ul class="nav clear" id="nav">
        	<li><a href="index.html" rel="external nofollow" rel="external nofollow" >首頁</a></li>
        	<li><a href="1.html" rel="external nofollow" rel="external nofollow" >欄目一</a></li>
        	<li><a href="2.html" rel="external nofollow" rel="external nofollow" >欄目二</a></li>
        	<li><a href="3.html" rel="external nofollow" rel="external nofollow" >欄目三</a></li>
        	</ul>
        <h1>欄目一</h1>
        </body>
        <script type="text/javascript" src="js/jquery-1.7.min.js"></script>
        <script type="text/javascript">
        	var urlstr = location.href;
        	console.log(urlstr+'/');
         var urlstatus=false;
         $("#nav a").each(function () { 
         if ((urlstr + '/').indexOf($(this).attr('href')) > -1&&$(this).attr('href')!='') {
         $(this).addClass('active'); urlstatus = true;
         } else {
         $(this).removeClass('active');
         }
         });
         if (!urlstatus) {$("#nav a").eq(0).addClass('active'); }
        </script>
        </html>
        

        效果圖

        注意:

        1、 location.href 用于獲取當(dāng)前頁面的url

        2、 當(dāng)前頁面應(yīng)該保存為index.html

        3、 indexOf 用于檢索當(dāng)前url中是否存在a中對應(yīng)的href

        4、 每個html中都擁有相同的導(dǎo)航結(jié)構(gòu)

        5、 eq(index/-index) 獲取當(dāng)前鏈?zhǔn)讲僮髦械贜個JQuery對象,返回JQquery對象,當(dāng)參數(shù)大于等于0時為正向選取,比如0代表第一個,1代表第二個。當(dāng)參數(shù)為負(fù)數(shù)時為反向選取,比如-1代表倒數(shù)第一個。

        拓展知識:js實現(xiàn)導(dǎo)航菜單點擊切換選中時高亮狀態(tài)方法

        通過 include() 或require() 函數(shù),您可以在服務(wù)器執(zhí)行 PHP 文件之前在該文件中插入一個文件的內(nèi)容。

        除了它們處理錯誤的方式不同之外,這兩個函數(shù)在其他方面都是相同的。

        include() 函數(shù)會生成一個警告(但是腳本會繼續(xù)執(zhí)行),

        而 require() 函數(shù)會生成一個致命錯誤(fatal error)(在錯誤發(fā)生后腳本會停止執(zhí)行)。

        這兩個函數(shù)用于創(chuàng)建可在多個頁面重復(fù)使用的函數(shù)、頁眉、頁腳或元素。

        這會為開發(fā)者節(jié)省大量的時間。

        這意味著您可以創(chuàng)建供所有網(wǎng)頁引用的標(biāo)準(zhǔn)頁眉或菜單文件。當(dāng)頁眉需要更新時,您只更新一個包含文件就可以了,或者當(dāng)您向網(wǎng)站添加一張新頁面時,僅僅需要修改一下菜單文件(而不是更新所有網(wǎng)頁中的鏈接)。

        這時就會出現(xiàn)這樣的問題:導(dǎo)航高亮應(yīng)該怎樣處理?

        公共代碼提出后就不可能在每個頁面的導(dǎo)航欄目后加class=“active”屬性進(jìn)行修改,這時就需要使用javascript來搞定。

        代碼如下:

        <script type="text/javascript" src="http://www.daixiaorui.com/Public/js/jquery.min.js"></script>
        <style>
         .menu { padding:0; margin:0; list-style-type:none;}
         .menu li { background:#FFD1A4; margin-right:1px; float:left; color:#fff; }
         .menu li a { display:block; width:80px; text-align:center; height:32px; line-height:32px; color:#fff; font-size:13px; text-decoration:none;}
         
         .cur{ background:#D96C00; font-weight:bold;}
        </style>
         
        <ul class="menu" id="menu">
         <li><a href="demo1.html?aa=1" rel="external nofollow" >首頁</a></li>
         <li><a href="demo1.html?aa=2" rel="external nofollow" >欄目一</a></li>
         <li><a href="demo1.html?aa=3" rel="external nofollow" >欄目二</a></li>
        </ul>
         
        <script type="text/javascript">
         var urlstr = location.href;
         //alert(urlstr);
         var urlstatus=false;
         $("#menu a").each(function () {
         if ((urlstr + '/').indexOf($(this).attr('href')) > -1&&$(this).attr('href')!='') {
         $(this).addClass('cur'); urlstatus = true;
         } else {
         $(this).removeClass('cur');
         }
         });
         if (!urlstatus) {$("#menu a").eq(0).addClass('cur'); }
        </script>

        運行效果:

        以上這篇使用JS實現(xiàn)導(dǎo)航切換時高亮顯示的示例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        使用JS實現(xiàn)導(dǎo)航切換時高亮顯示的示例講解

        使用JS實現(xiàn)導(dǎo)航切換時高亮顯示的示例講解:index.html代碼內(nèi)容 <!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>導(dǎo)航高亮顯示</title> <style type=text/css> body{ font-size:20
        推薦度:
        標(biāo)簽: 切換 導(dǎo)航 js
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲另类无码专区首页| 国产av天堂亚洲国产av天堂 | 国产亚洲精品美女2020久久| 337p日本欧洲亚洲大胆人人 | 人体大胆做受免费视频| 在线观看特色大片免费网站| 少妇人妻偷人精品免费视频| 午夜视频免费观看| 亚洲国产成人a精品不卡在线| 国产亚洲婷婷香蕉久久精品| 亚洲色大成WWW亚洲女子| 国精产品一区一区三区免费视频 | 成年男女免费视频网站| 国内精品久久久久久久亚洲| 亚洲va在线va天堂va手机| aa午夜免费剧场| 日韩吃奶摸下AA片免费观看| 亚洲精品二区国产综合野狼| 无码少妇精品一区二区免费动态 | 亚洲视频一区在线| 丁香花在线观看免费观看图片| 97在线线免费观看视频在线观看 | 黄色三级三级免费看| 无限动漫网在线观看免费| 亚洲乱色熟女一区二区三区蜜臀| 国产成人精品免费视频软件| 久久久无码精品亚洲日韩京东传媒| 久久A级毛片免费观看| 亚洲日韩激情无码一区| 免费国产草莓视频在线观看黄| 毛片免费在线播放| 黄色一级视频免费观看| 内射少妇36P亚洲区| 国产精品偷伦视频观看免费 | 免费v片在线观看视频网站| 亚洲中文久久精品无码ww16| 99ee6热久久免费精品6| 亚洲AV人无码激艳猛片| 在线免费视频你懂的| 亚洲AV中文无码乱人伦在线视色| 国产精品免费福利久久|