Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在整个引导过程中突出显示活动链接?_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

Javascript 如何在整个引导过程中突出显示活动链接?

Javascript 如何在整个引导过程中突出显示活动链接?,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,有人能帮我写一个脚本,让选中的链接保持高亮显示吗 <div id="productnav"> <nav> <ul class="nav"> <li><a href="<?php echo $prefix; ?>pages/one.php?category=1" id="navelement1"<?php if ($c == 1) echo ' class="current"';?>>One</a>&l

有人能帮我写一个脚本,让选中的链接保持高亮显示吗

<div id="productnav">
<nav>
<ul class="nav">
<li><a href="<?php echo $prefix; ?>pages/one.php?category=1" id="navelement1"<?php if ($c == 1) echo ' class="current"';?>>One</a></li>
<li><a href="<?php echo $prefix; ?>pages/two.php?category=2" id="navelement2"<?php if ($c == 2) echo ' class="current"';?>>Two</a></li>
<li><a href="<?php echo $prefix; ?>pages/three.php?category=3" id="navelement3"<?php if ($c == 3) echo ' class="current"';?>>Three</a></li>
<li><a href="<?php echo $prefix; ?>pages/four.php?category=4" id="navelement4"<?php if ($c == 4) echo ' class="current"';?>>Four</a></li></li>
</ul>
</nav>
</div>
这里的CSS

#productnav ul li a.active { border-right: solid 8px rgb(200,10,50);  text-decoration: none; background-color: #4E5765; !important; }

谢谢…

这是不可能的,因为当用户单击链接时,页面会发生变化,JavaScript无法“记住”用户以前单击过的链接,只是因为脚本已重新加载

你可以:

  • 将单击的链接存储在浏览器内存/Cookie中
  • 使用XHR和jQuery异步页面加载
这是不可能的,因为当用户单击链接时,页面会发生变化,JavaScript无法“记住”用户以前单击过的链接,只是因为脚本已重新加载

你可以:

  • 将单击的链接存储在浏览器内存/Cookie中
  • 使用XHR和jQuery异步页面加载
如果可以通过PHP将活动类添加到包含当前页面链接的列表项中,请执行以下操作:

.navbar导航li.active a{
背景:rgba(0,0,0,1);
}

如果可以通过PHP将活动类添加到包含当前页面链接的列表项中,请执行以下操作:

.navbar导航li.active a{
背景:rgba(0,0,0,1);
}


您可以使用JQuery添加活动类:

函数activeClass(){
var fullCurrentURL=window.location.href;
var CurrentURLparts=fullCurrentURL.split(“/”);
var CurrentURLindex=CurrentURLparts.length-1;
var CurrentURL=CurrentURLparts[CurrentURLindex];
console.log(CurrentURL);
$('.navbar nav>li a')。每个(函数(){
var fullLinkURL=$(this.attr('href');
var LinkURLparts=fullLinkURL.split(“/”);
var LinkURLindex=LinkURLparts.length-1;
var LinkURL=LinkURLparts[LinkURLindex];
if(LinkURL==CurrentURL){
$(此).parents(“li”).addClass(“活动”);
$(this).closest(“ul”).css('display','block');
}
});
}
activeClass()
#产品导航ul li.active a{
右边框:实心8px rgb(200,10,50);
背景色:#4E5765;
颜色:#fff;
}


您可以使用JQuery添加活动类:

函数activeClass(){
var fullCurrentURL=window.location.href;
var CurrentURLparts=fullCurrentURL.split(“/”);
var CurrentURLindex=CurrentURLparts.length-1;
var CurrentURL=CurrentURLparts[CurrentURLindex];
console.log(CurrentURL);
$('.navbar nav>li a')。每个(函数(){
var fullLinkURL=$(this.attr('href');
var LinkURLparts=fullLinkURL.split(“/”);
var LinkURLindex=LinkURLparts.length-1;
var LinkURL=LinkURLparts[LinkURLindex];
if(LinkURL==CurrentURL){
$(此).parents(“li”).addClass(“活动”);
$(this).closest(“ul”).css('display','block');
}
});
}
activeClass()
#产品导航ul li.active a{
右边框:实心8px rgb(200,10,50);
背景色:#4E5765;
颜色:#fff;
}

选择器

const selector = `nav a[href^="/${location.pathname.split("/")[1]}"]`;
jQuery

$(selector).addClass('active');
香草js

Array.from(document.querySelectorAll(selector))
    .forEach(a => a.classList.add('active'));
选择器

const selector = `nav a[href^="/${location.pathname.split("/")[1]}"]`;
jQuery

$(selector).addClass('active');
香草js

Array.from(document.querySelectorAll(selector))
    .forEach(a => a.classList.add('active'));

请将您的代码压缩到一个代码段,您不需要3请在代码段中只编写HTML而不是PHP。您是否已调试以了解为什么某些链接可能无法通过indexOf检查?您正在比较HREF以查看它是否包含当前页面URL,并突出显示这些链接。可能只有1个链接成功通过了测试,并被分配了“活动”类。您是否检查了元素以查看是否添加了“active”类?您是否尝试了我给您的任何代码段?请将您的代码压缩为一个代码段,您不需要3请在代码段中只编写HTML而不是PHP。您是否调试过以了解为什么某些链接可能无法通过indexOf检查?您正在比较HREF以查看它是否包含当前页面URL,并突出显示这些链接。可能只有1个链接成功通过了测试,并被分配了“活动”类。您是否检查了元素以查看是否添加了“active”类?您是否尝试过我给您的任何代码片段?您好,Syncro,您使用cookie的想法可能是正确的,因为(上面)没有其他东西对我有效。你能给我一个关于如何在嵌入cookie后编写java代码的提示吗?你是指javascript吗?你好,Syncro,你使用cookie的想法可能是对的,因为上面没有其他东西对我有用。你能给我一个关于如何在嵌入cookie后编写java代码的提示吗?你是说javascript吗?我没有想到这种方法:)很好!没想到这个方法:)不错!