jQuery 加载时突出显示导航项
jQuery Highlight Nav Item on Load
我是 HTML/CSS/JS 的新手,我正在做一些测试和摆弄一些东西,并决定在该页面上突出显示导航元素。我决定为此使用 jQuery ,因为从我读到的内容来看,这是最简单的解决方案。到目前为止它运行良好,但我的问题是它没有在加载时突出显示主 "chat" 页面,因为它只是 url,没有 /index.html。对于如何将活动 class 添加到加载时的 index.html 元素的任何帮助,我将不胜感激。
HTML:
<aside>
<nav>
<ul>
<li><a href="/index.html">Chat</a></li>
<li><a href="/friends.html">Friends</a></li>
<li><a href="/settings.html">Settings</a></li>
</ul>
</nav>
</aside>
JQuery:
jQuery(function($) {
var path = window.location.href;
$('aside nav ul li a').each(function() {
if (this.href === path) {
$(this).addClass('active');
}
});
SCSS:
.active {
border-right: 10px solid $accent-two;
}
我给你举个例子说明我将如何打点它。请注意,我不得不用一些代码模拟位置变化,但每次你 运行 这个例子,它都会突出显示一个不同的锚标记。
$(document).ready(function()
{
// Simulate change on "windows.location.href"
//===========================================================
var paths = ["/index.html","/friends.html","/settings.html"];
var idx = Math.floor(Math.random() * 3) + 0;
console.log("Random idx = " + idx);
//===========================================================
var path = paths[idx];
//var path = window.location.href;
$("a").removeClass('active');
$("a[href='" + path + "']").addClass('active');
});
.active {
border-right: 10px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<aside>
<nav>
<ul>
<li><a href="/index.html">Chat</a></li>
<li><a href="/friends.html">Friends</a></li>
<li><a href="/settings.html">Settings</a></li>
</ul>
</nav>
</aside>
首先,您的路径名包含整个 URL,因此它永远不会与您的 a 标记中的 href 匹配。您只需要获取页面名称。
接下来,您应该检查 a 标签的 href 属性,而不是 href 本身。
下面的示例使用虚拟 link 作为演示来提供帮助。由于代码段,我不得不在 JS 中添加 href。
jQuery(function($) {
var path = window.location.pathname;
var page = path.split("/").pop();
//
// The line below is for demo on Whosebug purposes
$("#forDemoOnStackPurpose").attr("href", page);
//
$('aside nav ul li a').each(function() {
if ($(this).attr("href") === page) {
$(this).addClass('active');
}
});
});
.active {
border-right: 10px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<aside>
<nav>
<ul>
<li><a id="forDemoOnStackPurpose" href="">This test page</a></li>
<li><a href="index.html">Chat</a></li>
<li><a href="friends.html">Friends</a></li>
<li><a href="settings.html">Settings</a></li>
</ul>
</nav>
</aside>
我是 HTML/CSS/JS 的新手,我正在做一些测试和摆弄一些东西,并决定在该页面上突出显示导航元素。我决定为此使用 jQuery ,因为从我读到的内容来看,这是最简单的解决方案。到目前为止它运行良好,但我的问题是它没有在加载时突出显示主 "chat" 页面,因为它只是 url,没有 /index.html。对于如何将活动 class 添加到加载时的 index.html 元素的任何帮助,我将不胜感激。
HTML:
<aside>
<nav>
<ul>
<li><a href="/index.html">Chat</a></li>
<li><a href="/friends.html">Friends</a></li>
<li><a href="/settings.html">Settings</a></li>
</ul>
</nav>
</aside>
JQuery:
jQuery(function($) {
var path = window.location.href;
$('aside nav ul li a').each(function() {
if (this.href === path) {
$(this).addClass('active');
}
});
SCSS:
.active {
border-right: 10px solid $accent-two;
}
我给你举个例子说明我将如何打点它。请注意,我不得不用一些代码模拟位置变化,但每次你 运行 这个例子,它都会突出显示一个不同的锚标记。
$(document).ready(function()
{
// Simulate change on "windows.location.href"
//===========================================================
var paths = ["/index.html","/friends.html","/settings.html"];
var idx = Math.floor(Math.random() * 3) + 0;
console.log("Random idx = " + idx);
//===========================================================
var path = paths[idx];
//var path = window.location.href;
$("a").removeClass('active');
$("a[href='" + path + "']").addClass('active');
});
.active {
border-right: 10px solid blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<aside>
<nav>
<ul>
<li><a href="/index.html">Chat</a></li>
<li><a href="/friends.html">Friends</a></li>
<li><a href="/settings.html">Settings</a></li>
</ul>
</nav>
</aside>
首先,您的路径名包含整个 URL,因此它永远不会与您的 a 标记中的 href 匹配。您只需要获取页面名称。 接下来,您应该检查 a 标签的 href 属性,而不是 href 本身。 下面的示例使用虚拟 link 作为演示来提供帮助。由于代码段,我不得不在 JS 中添加 href。
jQuery(function($) {
var path = window.location.pathname;
var page = path.split("/").pop();
//
// The line below is for demo on Whosebug purposes
$("#forDemoOnStackPurpose").attr("href", page);
//
$('aside nav ul li a').each(function() {
if ($(this).attr("href") === page) {
$(this).addClass('active');
}
});
});
.active {
border-right: 10px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<aside>
<nav>
<ul>
<li><a id="forDemoOnStackPurpose" href="">This test page</a></li>
<li><a href="index.html">Chat</a></li>
<li><a href="friends.html">Friends</a></li>
<li><a href="settings.html">Settings</a></li>
</ul>
</nav>
</aside>