【问题标题】:load hashtag url content加载主题标签 url 内容
【发布时间】:2012-09-09 20:52:25
【问题描述】:

这是我的JSFiddle。昨天我发布了如何create hashtag url 的问题。我得到了答案,但如果我刷新,它会从初始开始。

例如:如果我在#content 2 上刷新,它会显示#content 1。我不希望这样。我想我应该在加载事件中使用。但不知道如何在这里实现。有什么解决方案吗?请帮帮我。

【问题讨论】:

    标签: jquery hashtag


    【解决方案1】:

    如果您希望跨页面记住您的选择,您需要将选择存储在会话/服务器上的某处。重新加载页面会从头开始你的脚本,如果你想预先选择一些你需要从会话中取回数据的东西

    另一种选择是使用 javascript 将选择写入 cookie,并读取该 cookie 以设置初始选择

    You can learn about using a cookie here。该示例使用 cookie 来存储用户名,但您可以使用它来存储变量“content_selection”。然后,当您的页面加载时,读取 cookie 值并使用它来选择正确的初始内容。

    【讨论】:

      【解决方案2】:

      我已经在Create hashtag url using jquery 回答了这个问题。在这里分享这个是因为链接的线程最初只是关于使用哈希标签(实际上是片段标识符)更新 URL,但是关于在重新加载后保留内容的相同问题最终由该线程中的 OP 提出。

      对于那些不想额外点击的人来说,代码非常简单。只需在function setActive(i) { // codes } 之后添加以下内容:

              var url = window.location.hash; // retrieve current hash value
              if(url.indexOf('Content') != -1){ // do the following if URL's hash contains "Content"
                  // remove "#" symbol from retrieved hash value
                  var currentHash = window.location.hash.substring(1).split("#");
                  // remove "-" symbol from retrieved hash value
                  var contentTitle = currentHash.toString().replace(/-/g, ' ');
                  // store hash value in "actualContent" variable
                  var actualContent = "This is " + contentTitle;
                  // remove "selected" for every instance of "myclass" to hide content first
                  $(".myclass").removeClass("selected");
                  // find div that contains retrieved hash value's text stored in "actualContent" variable and add "selected" class to that div to display the correct content
                  $("div:contains('" + actualContent + "')").addClass("selected");
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多