2016-11-02 70 views
1

我在報頭中的菜單結構如下執行功能的onload只有第一次

$(document).ready(function(){ 
 
\t $('.dropdown').on('show.bs.dropdown', function(e){ 
 
\t \t $(this).find('.dropdown-menu').first().stop(true, true).slideDown(); 
 
\t }); 
 
    $('.dropdown').on('hide.bs.dropdown', function(e){ 
 
    \t $(this).find('.dropdown-menu').first().stop(true, true).slideUp(); 
 
    \t }); 
 

 
\t $('#entrar').click(function(e){ 
 
\t  $('#head').slideToggle(); 
 
    \t $(".boton").css("display","none"); 
 
    \t $("#secciones").slideToggle(); 
 
     $("#foot").slideToggle(); 
 
\t }); 
 
    $('#entrar').click(function() { 
 
    $('html, body').animate({ 
 
     scrollTop: $(".principal").offset().top + 70 
 
    }, 700); 
 
    }); 
 
}); 
 

 
$(document).ready(init); 
 
\t function init(){ 
 
    var sec = document.getElementById("secciones"); 
 
    var he = document.getElementById("head"); 
 
    var fo = document.getElementById("foot"); 
 
    he.style.display = "none"; 
 
    sec.style.display = "none"; 
 
    fo.style.display = "none"; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 
    <div class="main_wrapper"> 
 
    <header id="head"> 
 
\t \t <div class="container"> 
 
\t \t \t <section class="row"> 
 
\t \t \t \t <article class="col-xs-3"> 
 
\t \t \t \t \t <h1 id="main_logo"> 
 
\t \t \t \t \t \t <a href="index.php"><img src="images/logo.png" alt=""></a> 
 
\t \t \t \t \t </h1> 
 
\t \t \t \t </article> 
 
\t \t \t \t <article class="col-xs-9"> 
 
\t \t \t \t \t <ul class="nav nav-pills" id="menu"> 
 
\t \t \t \t \t \t <li class="active"><a href="index.php">INICIO</a></li> 
 
\t \t \t \t \t \t <li class="dropdown"> 
 
\t \t \t \t \t \t \t <a class="dropdown-toggle" data-toggle="dropdown" href="#">MODELOS<span class="caret"></span></a> 
 
\t \t \t \t \t \t \t <ul class="dropdown-menu"> 
 
\t \t \t \t \t \t \t \t <li><a class="sec_index" href="index.php#galeria">Modelos en Panamá</a></li> 
 
\t \t \t \t \t \t \t </ul> 
 
\t \t \t \t \t \t </li> 
 
\t \t \t \t \t \t <li class="dropdown"> 
 
\t \t \t \t \t \t \t <a class="dropdown-toggle" data-toggle="dropdown" href="#">BLOG<span class="caret"></span></a> 
 
\t \t \t \t \t \t \t <ul class="dropdown-menu"> 
 
\t \t \t \t \t \t \t \t <li><a class="sec_index" href="index.php#blog">Últimas Entradas</a></li> 
 
\t \t \t \t \t \t \t \t <li><a href="blog.php">Blog Chicas507</a></li> 
 
\t \t \t \t \t \t \t \t <li><a href="glosario.php">Glosario de Términos</a></li> 
 
\t \t \t \t \t \t \t </ul> 
 
\t \t \t \t \t \t </li> 
 
\t \t \t \t \t \t <li class="dropdown"> 
 
\t \t \t \t \t \t \t <a class="dropdown-toggle" data-toggle="dropdown" href="#">ANÚNCIATE<span class="caret"></span></a> 
 
\t \t \t \t \t \t \t <ul class="dropdown-menu"> 
 
\t \t \t \t \t \t \t \t <li><a class="sec_index" href="index.php#paquetes">Paquetes</a></li> 
 
\t \t \t \t \t \t \t \t <li><a href="registro.php">Regístrate</a></li> 
 
\t \t \t \t \t \t \t \t <li><a href="recuperar.php">Recuperar Cuenta</a></li> 
 
\t \t \t \t \t \t \t </ul> 
 
\t \t \t \t \t \t </li> 
 
\t \t \t \t \t \t <li><a class="sec_index" href="index.php#contacto">CONTACTO</a></li> 
 
\t \t \t \t \t \t <li><a href="about.php">INFORMACIÓN</a></li> 
 
\t \t \t \t \t \t <li><a href="login.php">INGRESA</a></li> 
 
\t \t \t \t \t </ul> 
 
\t \t \t \t </article> 
 
\t \t \t </section> 
 
\t \t </div> 
 
\t </header> 
 
    <section class="principal" id="principal"> 
 
\t \t \t <div class="boton" id="entrar"><a href="#">ENTRAR</a></div> 
 
\t \t \t <div class="boton" id="salir"><a href="#">SALIR</a></div> 
 
\t \t </section> 
 
\t <section class="secciones" id="secciones"> 
 
     some sections 
 
    </section> 
 
    <footer id="foot"> 
 
    </footer> 
 
    </div> 
 
</body>

而且我在網頁中各個部分,某些環節從菜單中獲得我到索引中的部分,例如索引#模型。一切正常,但這是事情。我有一些JavaScript代碼隱藏了部分和名爲init的頭文件。我在身體中使用onload來調用該函數並顯示一個按鈕「entrar」。這是有效的,但每次我重新加載頁面時都會調用該函數,而且我想僅在第一次運行時刷新頁面。一切都在顯示,而不點擊按鈕。另外,當我更改爲網站的其他部分時,我不想加載。例如,如果我進入博客,然後點擊回家,功能會加載,我必須再次點擊「入侵者」。我不想那樣。

我一直在努力解決這個問題,但是我找不到解決方案。我感謝您的幫助。請!

+0

我認爲你要找的是一個登錄功能。 –

+0

爲什麼你使用javascript來做一些CSS可以爲你做的事情?當然,它需要有一些服務器端來檢查是否顯示菜單或不是,但是...使用JavaScript顯示/隱藏菜單項是不好的設計,應該避免。 – junkfoodjunkie

+0

它不是,如果我只使用CSS,那麼每次加載頁面時,CSS都會應用,如果要控制何時執行該操作,則使用javascript做的想法。 –

回答

1

如果你想讓它堅持在瀏覽器關閉後,您可以設置一個localStorage的變種:

$(function() { 
    var visited = localStorage['visited']; 
    if (!visited) { 
     init(); 
     localStorage['visited'] = true; 
    } 

    function init() { 
     var sec = document.getElementById("secciones"); 
     var he = document.getElementById("head"); 
     var fo = document.getElementById("foot"); 
     he.style.display = "none"; 
     sec.style.display = "none"; 
     fo.style.display = "none"; 
    } 
}); 

或者sessionStorage的,如果你想在瀏覽器被關閉後,它的出現:

$(function() { 
    var visited = sessionStorage['visited']; 
    if (!visited) { 
     init(); 
     sessionStorage['visited'] = true; 
    } 

    function init() { 
     var sec = document.getElementById("secciones"); 
     var he = document.getElementById("head"); 
     var fo = document.getElementById("foot"); 
     he.style.display = "none"; 
     sec.style.display = "none"; 
     fo.style.display = "none"; 
    } 
}); 
+0

¡這個完美的作品!非常感謝 –

2

您可以使用HTML5 webstorage。

if (typeof(Storage) !== "undefined") 
    { 
     console.log("Your browser doesn't support webstorage"); 
    } 
    else if (sessionStorage.getItem("ui_flag") === null) // Check if the key is set. It's a self defined key. 
    { 
     sessionStorage.setItem("ui_flag", 1); // If not set, set it 
     $(document).ready(init); // Now call your init function here. This code will execute only once for a window. 
    } 
    else 
    { 
     //No Action. It's not the first time. 
    } 
0

我會去一個cookie,這意味着存儲一個小值,只要檢查它的存在。

與CSS結合,你會得到一個小而高效的解決方案。

隨着下面CSS/JS,當頁面加載第一次它創建的cookie,則第二次添加在loadedoncehtml元件,然後將CSS踢,示出了隱藏的元件

通過簡單地將Cookie的創建移動到按鈕點擊後,只有在點擊該按鈕後纔會顯示它們。

由於Cookie不會在堆棧段的工作,我用你的代碼

CSS

#secciones, #head, #foot, .loadedonce .boton { 
    display: none; 
} 
.loadedonce #secciones, .loadedonce #head, .loadedonce #foot { 
    display: block; 
} 

JS做了一個fiddle demo如果負載(無論是在頭部或年初您的js文件)

(function(d) { 
    if (document.cookie.indexOf("loadedonce=yes") < 0) { 
    document.cookie = "loadedonce=yes; expires=Thu, 31 Dec 2099 12:00:00 UTC; path=/";  
    } else { 
    d.classList.add('loadedonce');  
    } 
})(document.documentElement); 

JS如果點擊

(function(d) { 
    if (document.cookie.indexOf("loadedonce=yes") >= 0) { 
    d.classList.add('loadedonce');  
    } 
})(document.documentElement); 


$('#entrar').click(function() { 
    document.cookie = "loadedonce=yes; expires=Thu, 31 Dec 2099 12:00:00 UTC; path=/";  
});