简体   繁体   English

页面重新加载后,菜单活动状态保持在单击的链接上

[英]Menu active state remain on clicked link after the page reload

I'm having trouble with my menu. 我的菜单有问题。 i want the active state to remain on the last clicked link. 我希望活动状态保留在最后单击的链接上。

<ul id="nav">
    <li class="active"><a href="?field1="2"&field2="test">products </a></li>
    <li><a href="?field1="3"&field2="test2">products2 </a></li>
</ul>

when i click on products2 the table on the right side will populate data base on the field in url this will refresh the page. 当我单击product2时,右侧的表将在url字段中填充数据库,这将刷新页面。 how can i remain the clicked link have the active class? 如何保持点击的链接处于活动状态?

The following should be working: 以下应该工作:

var qs = decodeURIComponent(location.search);
$('a[href="' + qs + '"]').parents('li').addClass('active');

In location.search you will find the query-string which is appenden to your URL. location.search您将找到附加在URL后面的查询字符串。

decodeURIComponent will decode your URL so it avoids eg the %20 for a whitespace. decodeURIComponent将解码您的URL,因此避免了例如%20的空格。

$('a[href="' + qs + '"]') selects the a -tag which has a href -attribute corresponding to your query-string. $('a[href="' + qs + '"]')选择a具有-tag href对应到查询字符串-attribute。

But I would recommend to remove the extra " from your url-parameters inside your href : 但我建议从href内的url参数中删除多余的"

<ul id="nav">
    <li class="active"><a href="?field1=2&field2=test">products </a></li>
    <li><a href="?field1=3&field2=test2">products2 </a></li>
</ul>

If you can't remove the quotation marks you have to escape them for the attribute-selector to work. 如果无法删除引号,则必须将其转义才能使属性选择器起作用。


Reference 参考

location.search location.search

decodeURIComponent decodeURIComponent

jQuery attribute selector jQuery属性选择器

.parents() 。父母()

.addClass() .addClass()

You can examine the url, and with that apply the class to the relevant element. 您可以检查url,然后将类应用于相关元素。 This can be done server side, or client side (i see that you tag this with client technology) 这可以在服务器端或客户端完成(我看到您使用客户端技术对其进行了标记)

Like this 像这样

//if url contains "field1=3" then add the class "selected" to element "tab2"
if (window.location.href.indexOf("field1=3") > -1) {
   $("#tab2").addClass("selected")
}

The values and tabs should obviously be structured in some whay, but you get the gist. 值和制表符显然应该以某种方式进行结构化,但是您可以理解要点。

Using jQuery, you can try this for your scenario. 使用jQuery,您可以针对自己的情况尝试此操作。

   var currentPath="?"+window.location.href.split("?")[1];
   $("a[href='"+currentPath+"']").parent().addClass("active");

Simple but ugly solution is that On the product2 page add the code to manually highlight the link. 一个简单但难看的解决方案是,在product2页面上添加代码以手动突出显示链接。

Or if the problem is for all the links and there are n number of links will be available in future then your code should be more structured. 或者,如果问题出在所有链接上,并且将来有n个链接可用,则您的代码应更结构化。 In this case you can put the common code for handling this kind of common work (highlighting the links on side bar) in one of the common js file. 在这种情况下,您可以将用于处理这种常见工作的通用代码(突出显示侧栏上的链接)放在一个公共js文件中。 Instead of putting on all the pages. 而不是放在所有页面上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM