Javascript 如何根据页面I';在Jquery中创建活动选项卡效果;我在吗?
有许多教程向您展示了如何通过从url获取#来在导航栏中创建活动选项卡效果 这是伟大的,如果你是链接到同一页,但如果你所有的链接是在不同的网页上。有没有办法使用Jquery获取url并激活选项卡 以下示例为特定选项卡提供了“活动”类: 如何修改此链接以使用指向网站内其他页面的链接?以下是html:Javascript 如何根据页面I';在Jquery中创建活动选项卡效果;我在吗?,javascript,jquery,css,navigation,Javascript,Jquery,Css,Navigation,有许多教程向您展示了如何通过从url获取#来在导航栏中创建活动选项卡效果 这是伟大的,如果你是链接到同一页,但如果你所有的链接是在不同的网页上。有没有办法使用Jquery获取url并激活选项卡 以下示例为特定选项卡提供了“活动”类: 如何修改此链接以使用指向网站内其他页面的链接?以下是html: <ul id="nav" class="tabs"> <li><a href="/" title="Home" class="active">Home<
<ul id="nav" class="tabs">
<li><a href="/" title="Home" class="active">Home</a></li>
<li><a href="/about/" title="About us">About</a></li>
<li><a href="/demo/" title="Demo">Demo</a></li>
<li><a href="/where/" class="dir" title="Where">Where</a></li>
<li><a href="/contact/" class="dir" title="Contact Us">Contact Us</a></li>
</ul>
上面的jquery只适用于#tab1、#tab2等,如何根据当前页面名称激活选项卡?抱歉,如果这有点不清楚,很难解释
location.href
将返回当前页面的URL,因此您可以将if语句更改为-
if (location.href.indexOf(contentLocation) != -1) {
这将检查单击的href的链接是否包含在当前页面的URL中。显然,您必须更改此逻辑以满足您自己的需要。在加载页面时,您可以检查正在显示的页面的“window.location”对象,然后将“active”类设置为相应的链接: $(document).ready(function () { //iterate through your links and see if they are found in the window.location.pathname string var loc_href = window.location.pathname; $('#nav a').each(function () { if (loc_href == $(this).attr('href')) { $(this).addClass('active'); } }); }); $(文档).ready(函数(){ //遍历链接,查看是否在window.location.pathname字符串中找到它们 var loc_href=window.location.pathname; $('#nav a')。每个(函数(){ 如果(loc_href==$(this.attr('href')){ $(this.addClass('active'); } }); }); 注意:loc=$(this).attr('href')行查看链接的href值是否与window.location.pathname字符串完全匹配 $(document).ready(function () { //iterate through your links and see if they are found in the window.location.pathname string var loc_href = window.location.pathname; $('#nav a').each(function () { if (loc_href == $(this).attr('href')) { $(this).addClass('active'); } }); });