简体   繁体   English

页面重新加载jQuery后如何显示div和当前链接处于活动状态

[英]How to show a div and the current link active after page reload jquery

I want to show a div and a link active which is clicked after page reload. 我想显示一个div和一个激活的链接,在页面重新加载后单击该链接。 Initially class active is called to first link and div is hidden. 最初,类active被调用到第一个链接,而div被隐藏。

I checked everything I could think of, but didn't get the solution as my code is not working after page reload. 我检查了所有可以想到的内容,但没有得到解决方案,因为页面重新加载后我的代码无法正常工作。

Please see the html and jquery code: 请查看html和jquery代码:

<div class="sub-cols" style="display:none;">
    <div class="wraper-berkowits">
    <a class="active" href="<?php echo Mage::getBaseUrl() ?>products-for-sale/attachments.html">Attachments</a>
    <a href="<?php echo Mage::getBaseUrl() ?>products-for-sale/removal.html">Removal</a></li>
    <a href="<?php echo Mage::getBaseUrl() ?>products-for-sale/hair-loss-store.html">Hair Loss</a></li>
    <a href="<?php echo Mage::getBaseUrl() ?>products-for-sale/care-maintenance.html">Care &amp; maintenance </a>
    </div>
</div>


jQuery(document).ready(function() {
    jQuery('.menu:first-child').addClass('first'); 

    jQuery('li.last a').click(function() {  
        jQuery('.sub-cols').show();
        return false;
    }); 

    jQuery(".sub-cols a").click(function () {        
        jQuery(this).addClass('active').siblings().removeClass('active'); 

    });
});

Please check the above code and advise me how to get the desired result. 请检查上面的代码,并告诉我如何获得所需的结果。

As from your comments the question is better understood now As your need is to regain the state of the link even after the page reload. 从您的评论中可以更好地理解问题,因为您的需要是即使在页面重新加载后也可以重新获得链接的状态。 I can provide you the hint of achieving it 我可以为您提供实现目标的提示

There are two ways - 1) you can $_SESSION in php to store the link clicked and can have it display using jquery 有两种方法-1)您可以在php中使用$ _SESSION存储单击的链接,并可以使用jquery显示它

2) Using javascript only - use window.localStorage – stores data with no expiration date 2)仅使用javascript-使用window.localStorage –存储没有到期日期的数据

When you need to set a variable that should be reflected in the next page(s), use: 当您需要设置一个应在下一页中反映的变量时,请使用:

var someVarName = "value";
localStorage.setItem("someVarName", someVarName);

In your code you can do something like this 在您的代码中,您可以执行以下操作

jQuery(".sub-cols a").click(function () {
        localStorage.setItem("someVarName", jQuery(this).text());
        jQuery(this).addClass('active').siblings().removeClass('active');
    });
    jQuery(".sub-cols").show();

And in any page (like when the page has loaded), get it like: 在任何页面(例如页面加载时)中,都可以像这样获得它:

var someVarName = localStorage.getItem("someVarName");

.getItem() will return null or the value stored. .getItem()将返回null或存储的值。

for your example you can use like this 对于您的示例,您可以像这样使用

if(localStorage.getItem("someVarName")!=null){
        $('a').filter(function(){
                return this.innerHTML == localStorage.getItem("someVarName"); 
            }).css({background:"#F00"});
    }

I have provided an updated demo - http://jsfiddle.net/bvqnzuwa/3/ To test it click on any link and then reload the page. 我提供了一个更新的演示-http: //jsfiddle.net/bvqnzuwa/3/要对其进行测试,请单击任何链接,然后重新加载页面。 You will see a background color on the link you clicked even after page reload. 即使重新加载页面后,您在单击的链接上也会看到背景色。

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

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