我在報頭中的菜單結構如下執行功能的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」。這是有效的,但每次我重新加載頁面時都會調用該函數,而且我想僅在第一次運行時刷新頁面。一切都在顯示,而不點擊按鈕。另外,當我更改爲網站的其他部分時,我不想加載。例如,如果我進入博客,然後點擊回家,功能會加載,我必須再次點擊「入侵者」。我不想那樣。
我一直在努力解決這個問題,但是我找不到解決方案。我感謝您的幫助。請!
我認爲你要找的是一個登錄功能。 –
爲什麼你使用javascript來做一些CSS可以爲你做的事情?當然,它需要有一些服務器端來檢查是否顯示菜單或不是,但是...使用JavaScript顯示/隱藏菜單項是不好的設計,應該避免。 – junkfoodjunkie
它不是,如果我只使用CSS,那麼每次加載頁面時,CSS都會應用,如果要控制何時執行該操作,則使用javascript做的想法。 –