html - 页面重新加载后选择某些部分元素

标签 html css tabs

我目前正在从事 html5 和 jquery 元素。我在页面上有一个 html5 选项卡 View ,每 5 秒刷新一次。它通过发布到从数据库中检索信息的 php 脚本来刷新,然后一些 javascript 将 php 脚本的打印输出添加到 div。在刷新之前,它使用 ajax 加载图像更新另一个 div,更新完成后,它使用   清除 div。由于刷新,虽然它总是默认为第一个选项卡,但很明显,但我需要在刷新后将其保留在选定的选项卡上。

下面是选项卡选择的代码。

function getEmailData()
{
    echo '
        <article class="tabs">
            <section id="tab1">
                <h2><a href="#tab1">Queued</a></h2>
                    <center><strong>Queued Emails</strong></center>
                ' . getEmails("Queued") . '
            </section>
            <section id="tab2">
                <h2><a href="#tab2">Trying</a></h2>
                <center><strong>Trying</strong></center>
                ' . getEmails("Trying") . '
            </section>
            <section id="tab3">
                <h2><a href="#tab3">Sent</a></h2>
                <center><strong>Sent Emails</strong></center>
                ' . getEmails("Sent") . '
            </section>
            <section id="tab4">
                <h2><a href="#tab4">Failed</a></h2>
                <center><strong>Failed Emails</strong></center>
                ' . getEmails("Failed") . '
            </section>
        </article>
    ';
} 

获取电子邮件();函数返回的数据应显示在每个选项卡中。

下面是CSS

article.tabs
{
    position: relative;
    display: block;
    width: 40em;
    height: 15em;
    margin: 2em auto;
}

article.tabs section
{
    position: absolute;
    display: block;
    /*top: 1.8em;*/
    top: -10px;
    left: 0;
    height: 12em;
    padding: 10px 20px;
    background-color: #ddd;
    border-radius: 5px;
    box-shadow: 0 3px 3px rgba(0, 0, 0, 0.1);
    z-index: 0;
    width: 800px;
    height: 300px;
}

article.tabs section:first-child
{
    z-index: 1;
        color: #333;
    background-color: #fff;
    z-index: 2;
}

article.tabs section h2
{
    position: absolute;
    font-size: 1em;
    font-weight: normal;
    width: 120px;
    height: 1.8em;
    top: -1.8em;
    left: 10px;
    padding: 0;
    margin: 0;
    color: #999;
    background-color: #ddd;
    border-radius: 5px 5px 0 0;
}


article.tabs section:nth-child(2) h2
{
    left: 132px;
}

article.tabs section:nth-child(3) h2
{
    left: 254px;
}

article.tabs section:nth-child(4) h2
{
    left: 376px;
}


article.tabs section h2 a
{
    display: block;
    width: 100%;
    line-height: 1.8em;
    text-align: center;
    text-decoration: none;
    color: inherit;
    outline: 0 none;
}

article.tabs section:target, article.tabs section:target h2
{
    color: #333;
    background-color: #fff;
    z-index: 2;
}

article.tabs section, article.tabs section h2
{
    -webkit-transition: all 500ms ease;
    -moz-transition: all 500ms ease;
    -ms-transition: all 500ms ease;
    -o-transition: all 500ms ease;
    transition: all 500ms ease;
}

基本上,我的问题是如果我选择第 3 个选项卡,并且 ajax post 重新加载,我如何确保仍然选择第 3 个选项卡并且不会默认返回到第一个选项卡。

感谢您提供的任何帮助。

最佳答案

据我所知,您有 3 个选择:

  1. 单独刷新每个标签,只刷新标签的内容,而不是整篇文章
  2. 在 Ajax 调用之前将先前选择的选项卡保存到 JavaScript 变量中,然后在接收并插入数据后重新选择选项卡
  3. 通过 ajax 调用发送预选选项卡,并在后端代码中的选项卡上插入“选定”类。

2 和 3 实际上是一样的 - 只是改变了数据的存储位置。

我的首选是第 1 项,因为它不需要 jQuery 来刷新选项卡对象,而且还减少了回传的数据量。如果您必须刷新整个 article,那么 2 或 3 就可以了(虽然 2 可能更容易实现!)

关于html - 页面重新加载后选择某些部分元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11318236/

相关文章:

html - 在网页上,如何为特定部分设置滚动条?

css - 如何制作星球大战片尾动画?

CSS 将鼠标悬停在表格中时更改表格中特定单元格的背景颜色

java - AlertDialog 中的 Android TabActivity

javascript - 如何在关闭浏览器而不是关闭选项卡时从 localStorage 中删除数据?

html - Bootstrap : Center a header within a div

html - 如何通过CSS重新组合文本?

css - 为什么 Chrome 比 Firefox 或 IE 包装内联列表项更多?

css - Angular 样式文本的变换、旋转和变换原点问题

php - 如何合并 WooCommerce 中的“描述”和“附加信息”选项卡?