简体   繁体   English

根据点击事件修改网址

[英]Modify url based on a click event

I have a series of divs that when clicked it slides up basically a modal. 我有一系列的divs ,当单击它们时,它们基本上是模态的。 The modal has a data-attribute that appears in the url. 模态具有出现在url中的data-attribute The issue I'm having is when I close the modal, the url still has the data-attribute hash, it doesn't revert back and I need it to. 我遇到的问题是,当我关闭模式时,URL仍然具有data-attribute哈希值,它不会还原并需要它。

This is the js: 这是js:

$('[data-agent]').click(function() {
        var element = $(this);
        var target = element.data('agent');
        $('body').addClass('agentLoaded');
        $('[data-agent-target]').not('[data-agent-target="'+target+'"]').removeClass('active');
        $('[data-agent-target="'+target+'"]').addClass('active');
        window.location.hash = 'agent='+target;
        return false;
    });

    if(document.location.hash){
        var hash = document.location.hash.split('#')[1];
        hash = hash.replace('agent=', '');
        if(hash && $('[data-agent-target="'+hash+'"]').length) {
            $('[data-agent="'+hash+'"]').trigger('click');
        }
    }

    $('.close').click(function() {
        $('[data-agent-target]').removeClass('active');
        $('body').removeClass('agentLoaded');
        return false;
    });

[edit] Please see the question in the code [编辑]请在代码中查看问题

$('[data-agent]').click(function() {
    var element = $(this);
    var target = element.data('agent');
    $('body').addClass('agentLoaded');
    $('[data-agent-target]').not('[data-agent-target="'+target+'"]').removeClass('active');
    $('[data-agent-target="'+target+'"]').addClass('active');
    window.location.hash = 'agent='+target;
    return false;
});

if(document.location.hash){ /* What is this for ? */
    var hash = document.location.hash.split('#')[1];
    hash = hash.replace('agent=', '');
    if(hash && $('[data-agent-target="'+hash+'"]').length) {
        $('[data-agent="'+hash+'"]').trigger('click');
    }
}

$('.close').click(function() {
    $('[data-agent-target]').removeClass('active');
    $('body').removeClass('agentLoaded');
    return false;
});

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

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