溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-08-26 07:57:07 來源:腳本之家 閱讀:207 作者:web_callBack 欄目:web開發(fā)

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>
<h2>欄目一</h2>
</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>

效果圖

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

注意:

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”屬性進行修改,這時就需要使用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)航切換時高亮顯示的示例講解

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

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI