Remove hashtag from url

一曲冷凌霜 提交于 2019-12-13 04:34:34

问题


Having a few issues with the code.

$(function () {
    var items = $('#v-nav>ul>li').each(function () {
        $(this).click(function () {
            //remove previous class and add it to clicked tab
            items.removeClass('current');
            $(this).addClass('current');

            //hide all content divs and show current one
            $('#v-nav>div.tab-content').hide().eq(items.index($(this))).show('fast');

            window.location.hash = $(this).attr('tab');
        });
    });

    if (location.hash) {
        showTab(location.hash);
    }
    else {
        showTab("tab1");
    }

    function showTab(tab) {
        $("#v-nav ul li[tab=" + tab + "]").click();
    }

    // stop the click on the link adding a # to the end of the 
    event.preventDefault();

    // Bind the event hashchange, using jquery-hashchange-plugin
    $(window).hashchange(function () {
        showTab(location.hash.replace("#", ""));
    })

    // Trigger the event hashchange on page load, using jquery-hashchange-plugin
    $(window).hashchange();

});

and this is the url http://www.r1hosting.net/vps-servers#tab1

I want to remove the #tab1, #tab2, #tab3, #tab4 and so fourth...

Any ideas? I've tried next to everything...


回答1:


If you don't want to use hashtags, you can easily remove this line:

window.location.hash = $(this).attr('tab');

If you remove that, this code would not do anything since the # is not set and can be removed aswell:

// Bind the event hashchange, using jquery-hashchange-plugin
$(window).hashchange(function () {
    showTab(location.hash.replace("#", ""));
})

// Trigger the event hashchange on page load, using jquery-hashchange-plugin
$(window).hashchange();



回答2:


You need to move the event.preventDefault() within a click handler that receives the event.

$(this).click(function (event) {
    event.preventDefault();

    // rest of the handler code goes here...
}


来源:https://stackoverflow.com/questions/21268075/remove-hashtag-from-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!