【问题标题】:Jquery make tab active from another pageJquery使标签从另一个页面处于活动状态
【发布时间】:2016-10-02 10:04:54
【问题描述】:

所以我在另一个页面上有一个按钮,当用户单击它时,它应该重定向到另一个页面并将活动类添加到 window.location.hash 采用的选项卡中......但它不起作用.它只是重定向到该页面。我做错了什么?

HTML:

/* JQUERY */

$("#butonmesaj").on('click', function(){
     window.location.href = "{% url 'userena_profile_detail' user%}#messages";
     var id = window.location.hash;
     $(id).addClass('active');
});
<div role="tabpanel" class="tab-pane" id="messages">
     <div class="embed-responsive embed-responsive-16by9" style="height: 500px;">
     <iframe class="embed-responsive-item" src="{% url 'userena_umessages_list' %}"></iframe>
</div>

【问题讨论】:

    标签: javascript jquery html css django


    【解决方案1】:

    您在 url 上正确设置了哈希,但是您需要在加载页面时运行 hashChange 函数以实际设置加载时的类。这是因为当您更改 url 时,您正在重新初始化您的应用程序并丢失您没有通过请求传递回服务器的任何状态。

    // callback to set active class based on hash
    function hashChange() {
      var id = window.location.hash
      $(id).addClass('active')
    }
    // bind the callback to hashchange and window.onload
    $(window).on('hashchange', hashChange)
    $(hashChange)
    
    // your standard click listener
    $("#butonmesaj").on('click', function() {
      window.location.href = "{% url 'userena_profile_detail' user%}#messages"
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多