用x关闭或隐藏信息框

Closing or hiding an info box with an x

本文关键字:隐藏 信息      更新时间:2023-09-26

我试着寻找线索,但留下了一大块空白。可能我找错东西了!

我在一个页面上创建了一个基本信息块。

我想让用户可以通过点击x.来隐藏或关闭该框

我假设我需要使用cookie,但不知道从哪里开始!也许是jquery,因为我在每个页面上都加载了其他元素的jquery?

一个重要因素是,当显示新信息时,它应该重新出现。

这是怎么做到的?有人能帮忙吗?

谢谢!

您可以使用这样的东西:

<div id="content" style="display:none;">
    <span class="close">&times;</span>
    Some text
</div>
<script>
    jQuery(document).ready(function(){
        if (!readCookie('messageClosed')){
            jQuery('#content').show();
        }
        jQuery('#content .close').click(function(){
            jQuery('#content').slideUp();
            createCookie('messageClosed', 1, 365);
            return false;
        });
    });
</script>

函数readCookie和createCookie如下所示:https://stackoverflow.com/a/1460174/2828391

一旦你阅读了Nicolas为你链接的初学者javascript指南,也许还有jQuery教程,你可能会写这样的东西。

Javascript

jQuery('div.xbutton').click(function(){
jQuery('div.infoblock').hide();
});

HTML

<div class="infoblock">Information text <div class="xbutton">X</div></div>

但这是在未来,所以从阅读指南开始。这将非常有用:)