简体   繁体   English

在该人选择隐藏div之后,如何使div重新出现? (与javascript相关)

[英]How can I make the div reappear again, after the person chose to hide the div? (javascript-related)

So I am javascript illiterate, but I did find this useful code online: 所以我是javascript文盲,但是我在网上确实找到了这个有用的代码:

<script type="text/javascript">
function readCookie( cName )
{
  var v;

  return decodeURIComponent( ( v = ( document.cookie || "" ).match( "(^|\\s)" + cName + "=([^;$]+)" ) ) ? v[ 2 ] : "" );
}

function setCookie( cName, cValue, life, cPath, cDomain, cSecure )
{
 var dt = new Date(), 
     expSecs = ( expSecs = life.toString().match( /\bsecs\s*=\s*(\d+)/i ) ) ? Number( expSecs[ 1 ] ) : 0,
     params = ( life ? ( ";expires=" + new Date( expSecs ? ( dt.setTime( dt.getTime() + expSecs * 1000 ) )                                                          : ( dt.setDate( dt.getDate() + life) ) ).toUTCString() ) : "" )
            + ( cPath ? (";path=" + cPath) : "" )
            + ( cDomain ? ";domain=" + cDomain : "" )
            + ( cSecure ? ";secure" : "" );

 document.cookie = cName + "=" + encodeURIComponent( cValue ) + params;

 return readCookie( cName );
}

if( readCookie( "close" ) )
{
  document.getElementById("thing").style.display="none";
}

function close_thing()
{
  document.getElementById("thing").style.display="none";
  setCookie( "close", "true", 20 );
}
</script>

It works with this div 它适用于这个div

<div id="thing">
<div id="close_stuff" onclick="close_thing();"><a href="#">Close this</a></div>
</div>

My problem is, if I want my div to contain content, which the user can close if he/she wishes. 我的问题是,如果我希望div包含内容,则用户可以根据需要关闭该内容。 However, I want the div to reappear, when I put new content into the div. 但是,当我在div中添加新内容时,我希望div重新出现。

Is there a way I could do this manually? 有没有办法我可以手动执行此操作? Like rename all the "close_thing" to "klose_thing", or something? 就像将所有“ close_thing”重命名为“ klose_thing”一样?

Thanks! 谢谢!

Adding another function will do this for you. 添加其他功能将为您完成此任务。 Set the display property to block. 将显示属性设置为“阻止”。

function show_thing()
{
  document.getElementById("thing").style.display="block";
}

Then call show_thing function display the div again. 然后调用show_thing函数再次显示div。

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

相关问题 如何清除内容div并使用JavaScript重新显示 - How to clear content div and reappear with javascript 使用javascript隐藏/显示CSS div-隐藏后不再显示- - Hide/Show css div with javascript - does not show again after hide - 如何制作 <div> 消失然后重新出现onclick - How to make a <div> disappear then reappear onclick 在移动浏览器上点击“后退”按钮后,如何再次从页面隐藏,浮动的“加载” div隐藏? - How do I make a hidden, floating “loading” div hide again from a page after hitting the back button on mobile safari? 一次又一次单击后添加/隐藏div - Add/Hide a div after click again and again 如何使用jQuery隐藏div,然后通过按其他div再次显示它? - How can I hide my div using jQuery, and then showing it again by pressing a different div? 如何使用jQuery Cycle插件隐藏div并使其在一段时间后再次可见? - How to hide the div and make it visible again after some interval while using jquery cycle plugin? 如何使div隐藏onclick并在再次单击时显示它? - How do I make a div hide onclick and make it show when it's clicked again? 如何在单击事件javaScript后隐藏div - How to hide the div after on click event javaScript 发生onclick事件后,如何使用Cookie隐藏div? - How can I hide a div using cookies after an onclick event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM