简体   繁体   English

show display:刷新后没有div

[英]show display:none div after refresh

Don't know how to put the question title correctly! 不知道如何正确地提出问题标题! I have a div that is displayed by button click. 我有一个按钮点击显示的div。 The problem is that if user goes to next page(by clicking another button) and then come back to the old page the div is not shown because page is refreshed, so user needs to click the button again to see the div. 问题是,如果用户转到下一页(通过单击另一个按钮)然后返回到旧页面,则不显示div,因为页面已刷新,因此用户需要再次单击该按钮才能看到div。

Is there any way to keep div displayed after button clicked for first time? 在第一次点击按钮后有没有办法保持div显示?

<div id="tableDiv" style="display:none;" >
<table>
   <td>something</td>
</table>
</div>

<input type="button" value="Show" onClick="showTable()"/>

<script type="text/javascript">         
   function showTable() {
       document.getElementById('tableDiv').style.display = "block";
   }                
</script>

You can use the html5 webStorage for this: 你可以使用html5 webStorage:

localStorage does not expire, whereas sessionStorage gets deleted when the browser is closed (usage of both is equivalent). localStorage不会过期,而在浏览器关闭时会删除sessionStorage (两者的使用是等效的)。 The webStorage is support by all major browsers and IE >= 8 所有主流浏览器都支持webStorage,IE> = 8

Plain Javascript 简单的Javascript

function showTable() {
   document.getElementById('tableDiv').style.display = "block";
   localStorage.setItem('show', 'true'); //store state in localStorage
}

And check the state onLoad: 并检查状态onLoad:

window.onload = function() {
    var show = localStorage.getItem('show');
    if(show === 'true'){
         document.getElementById('tableDiv').style.display = "block";
    }
}

jQuery jQuery的

function showTable() {
    $('#tableDiv').show();
    localStorage.setItem('show', 'true'); //store state in localStorage
}

$(document).ready(function(){
    var show = localStorage.getItem('show');
    if(show === 'true'){
        $('#tableDiv').show();
    }
});

Demo 演示

PS To remove an item from the localStorage use PS从localStorage使用中删除项目

localStorage.removeItem('show');

Reference 参考

webStorage webStorage

Use localstorage to save the state 使用localstorage来保存状态

<script type="text/javascript">         
   function showTable() {
       document.getElementById('tableDiv').style.display = "block";
       localStorage.setItem('showTable', true);  //set flag   
   }

   function hideTable() {
       document.getElementById('tableDiv').style.display = "none";
       localStorage.removeItem('showTable');  //remove key   
   }

   if (localStorage.getItem('showTable')) {  //see if flag is set (returns undefined if not set)
       showTable();   //if set show table
   }
</script>

I think your best option is to use the location.hash as a JavaScript Router. 我认为您最好的选择是将location.hash用作JavaScript路由器。 Basically modify the hash, watch for hash changes and if the hash is equal to a specific value do something. 基本上修改散列,监视散列更改,如果散列等于特定值做某事。 Then when the used leaves and hits "back", they will come back to the page with the previous hash, in which case you can detect which version of the page they were on and recreate it. 然后当使用的离开并点击“返回”时,它们将返回到具有前一个散列的页面,在这种情况下,您可以检测它们所在页面的哪个版本并重新创建它。

<div id="tableDiv" style="display:none;" >
  <table>
    <td>something</td>
  </table>
</div>
<input type="button" value="Show" onClick="showTable()"/>

<script type="text/javascript">
  function showTable(){
    location.hash = "#show"; // sets hash to #show, which will fire onhaschange event which its handler is hashRouter()
  }
  function hashRouter(){
    if(window.hash == "#show"){ // shows table if hash is #show
      document.getElementById('tableDiv').style.display = "block";
    }
  }
  window.onhashchange = hashRouter; // Calls when hash is changed.
  window.onload = hashRouter; // Calls when page loads;
</script>

There are many other options such as cookies or localstorage. 还有许多其他选项,例如cookie或localstorage。

Check out this plugin: 看看这个插件:

https://github.com/addcms/addHashRouter https://github.com/addcms/addHashRouter

Using this solution you might do something like this: 使用此解决方案,您可能会执行以下操作:

HTML HTML

<div id="tableDiv" style="display:none;">
  <table>
    <td>something</td>
  </table>
</div>
<a href='#show'>Show Table</a>

JavaScript JavaScript的

$add.UI.hashRouter.add("show", function(){
    document.getElementById('tableDiv').style.display = "block";
});

And then if they hit the "back button" after navigating away from the page it will still appear, and if they hit the back button after showing the table it will not "rehide" it, unless you added this: 然后如果他们在离开页面后点击“后退按钮”它仍会出现,如果他们在显示表后点击后退按钮,它将不会“重新隐藏”它,除非你添加了这个:

HTML HTML

<a href='#hide'>Hide Table</a>

JavaScript JavaScript的

$add.UI.hashRouter.add("hide", function(){
    document.getElementById('tableDiv').style.display = "none";
});

And then you can use show/hide buttons with browser navigation. 然后你可以使用浏览器导航的显示/隐藏按钮。

Using @DustinPoissant's answer, I've made something a little bit easier. 使用@ DustinPoissant的答案,我做了一些更容易的事情。

You can use the selector :target to style the element, and save you some code. 您可以使用selector :target来为元素设置样式,并为您节省一些代码。

Like this: 像这样:

<style>
    #tableDiv {display:none;}
    #tableDiv:target {display:block!important;}
</style>

<div id="tableDiv">
    <table>
        <tr>
            <td>something</td>
        </tr>
    </table>
</div>

<input type="button" value="Show" onClick="showTable()"/>
<input type="button" value="Hide" onClick="hideTable()"/>

<script type="text/javascript">
   function showTable() {
       location.hash='tableDiv';
   }
   function hideTable() {
       location.hash='';
   }
</script>

The :target selector will match when the 'hash' on your address matches the id of that element. 当地址上的'hash'与该元素的id匹配时, :target选择器将匹配。

you can achieve it with a cookie. 你可以用cookie实现它。 there's a good lightweight jquery cookie plugin for this. 这是一个很好的轻量级jquery cookie插件

so set a cookie: 所以设置一个cookie:

$.cookie("var", "10");

to get the cookie: 获取cookie:

$.cookie("var");

to delete the cookie: 删除cookie:

$.cookie("var", null);

and now you can do something like: 现在你可以这样做:

function showTable() {
    document.getElementById('tableDiv').style.display = "block";
    $.cookie("var", "1");
}

function leaveButtonOpen(){
    if($.cookie("var") == 1){
        document.getElementById('tableDiv').style.display = "block";
    }
}

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

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