欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年08月22日 17:00:52   作者:web_callBack  
今天小編就為大家分享一篇使用JS實(shí)現(xiàn)導(dǎo)航切換時(shí)高亮顯示的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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中對(duì)應(yīng)的href

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

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

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

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

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

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

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

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

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

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

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

公共代碼提出后就不可能在每個(gè)頁面的導(dǎo)航欄目后加class=“active”屬性進(jìn)行修改,這時(shí)就需要使用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>

運(yùn)行效果:

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

相關(guān)文章

最新評(píng)論