【问题标题】:Remove hashtag from url从网址中删除主题标签
【发布时间】:2014-01-22 04:16:36
【问题描述】:

代码有一些问题。

$(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();

});

这是网址http://www.r1hosting.net/vps-servers#tab1

我想删除#tab1、#tab2、#tab3、#tab4等等第四个...

有什么想法吗?我已经尝试过所有的东西......

【问题讨论】:

    标签: jquery web hashtag


    【解决方案1】:

    如果您不想使用主题标签,可以轻松删除此行:

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

    如果您删除它,此代码将不会执行任何操作,因为 # 未设置并且也可以删除:

    // 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】:

    您需要在接收事件的点击处理程序中移动event.preventDefault()

    $(this).click(function (event) {
        event.preventDefault();
    
        // rest of the handler code goes here...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      相关资源
      最近更新 更多