简体   繁体   中英

Selecting certain section element after page reload

I am currently working on a html5 and jquery project. I have an html5 tab view on a page which keeps refreshing every 5 seconds. It refreshes by posting to a php script which retrieves information from the database and then some javascript adds the print out from the php script to a div. Before the refresh it updates another div with an ajax loading image, and once the update is complete it clears the div with   . Because of the refresh though it always defaults to the first tab, obviously, but I need to keep it on the selected tab after the refresh.

Below is the code for the tab selection.

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>
    ';
} 

getEmails(); function returns the data should be displayed within each tab.

Below is the 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;
}

Basically, my question is if I select the 3rd tab, and the ajax post does a reload, how do I make sure that the 3rd tab is still selected and doesn't default back to the first.

Thanks for any help you can provide.

As far as I see it, you have 3 options:

  1. refresh each tab individually and only refresh the contents of the tab, not the entire article
  2. Save the previously selected tab into a JavaScript variable before the Ajax call and then re-select the tab after the data is received and inserted
  3. Send the pre-selected tab through the ajax call and insert a 'selected' class on the tab in the back end code.

2 and 3 are effectively the same - just changing where the data is stored.

My preference would be number 1 as it does not require the jQuery to refresh the tab object and also reduces the amount of data posted back. If you must refresh the entire article , though, 2 or 3 would do (2 is probably easier to implement though!)

You should create a cookie via PHP or javascript setting which tab is currently selected. try something like:

</script src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>

$('.tabs section h2 a').click(function(){
  var tabIndex = $(this).parents('section').first().addClass('selected').index();
  $.cookies.set( 'tabIndex', tabIndex );
})

then on document load

var tabIndex = $.cookies.get('tabIndex')
if( tabIndex ) {
   $('.tabs section:nth-child(' +tabIndex+ ')' ).addClass('selected')
}

disclaimer : not tested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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