2017-05-29 82 views
0

我正在面對基於Bootstrap的下拉菜單的問題(沒有什麼奇特的,只是一個導航)。一切工作順利,但是當我想通過單擊它來摺疊下拉菜單時,它會消失,但是當我將鼠標懸停或再次單擊時會顯示。我已經閱讀了關於stopPropagation()函數的一些線程,但它仍然不起作用。這是我的代碼。Bootstrap的下拉菜單在兩次點擊後消失

     <!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <meta name="description" content=""> 
     <meta name="author" content="Matt O'Such"> 

     <title></title> 
     <script src="js/jquery-3.2.1.min.js"></script> 
     <script src="js/bootstrap.min.js"></script> 
     <link rel="stylesheet" href="css/bootstrap.min.css"> 
     <link rel="stylesheet" href="css/normalize.css"> 
     <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet"> 
     <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet"> 

     <style> 
     body { 
     background-image: linear-gradient(to bottom, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
     background-image: -moz-linear-gradient(top, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
     background-image: -o-linear-gradient(top, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
     background-image: -ms-linear-gradient(top, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
     background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(51, 51, 51, 0.8)), to(rgba(51, 51, 51, 0.8))), url("images/background-homepage.jpg"); 
     background-image: -webkit-linear-gradient(top, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
     filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/background-homepage.jpg', sizingMethod='scale'); 
     -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/background-homepage.jpg', sizingMethod='scale')"; 
     } 

     .navbar-default .navbar-nav>li>a 
     { 
     color:#e6e6e6; 
     font-size:11px; 
     padding-top:22px; 
     } 

     .navbar-default .navbar-nav>li>a:hover, .navbar-default .navbar-nav>.active>a, .navbar-default .navbar-nav>.active>a:focus, .navbar-default .navbar-nav>.active>a:hover { 
     color:#f9f9f9; 
     background:#2f2b2e; 
     border-bottom:2px solid #bfff51; 
     font-size:11px; 
     } 

     .navbar-default .navbar-nav>.open>a, .navbar-default .navbar-nav>.open>a:focus, .navbar-default .navbar-nav>.open>a:hover{ 
     color:#f9f9f9; 
     background:#2f2b2e; 
     border-bottom:2px solid #bfff51; 
     font-size:11px; 
     } 

     .dropdown-menu>li>a { 
     display: block; 
     padding: 3px 20px; 
     clear: both; 
     font-weight: 400; 
     line-height: 1.42857143; 
     color: #fefefe; 
     white-space: nowrap; 
     background:#4c4649; 
     font-size:11px; 
     } 

     .dropdown-menu { 
     position:absolute; 
     padding:0px; 
     border-radius:0px; 
     border:0px; 
     min-width:0; 
     background:red;font-size:11px; 

     } 

     .wrapper { 
     } 

     .homepage-welcome { 
     font-family:Lobster; 
     font-size:2em; 
     color:#fefefe;} 

     .homepage-title{ 
     font-family:'Open Sans'; 
     font-size:3em; 
     color:#fefefe;} 
     } 

     </style> 

     <script> 
     $(document).ready(function() { 
      //$(".nav li").removeClass("active"); 
      $('.menu-item-home').addClass('active'); 
      //for demo 
      //$('#demo').addClass('active'); 
      //for sale 
      //$('#sale').addClass('active'); 
      $('.dropdown-menu input').click(function(e) { 
       e.stopPropagation(); 
      }); 
      }); 
     </script> 
    </head> 
    <body> 
     <div class="wrapper"> 
      <nav class="navbar navbar-default" style="border-radius:0px;background:none;border:0px;"> 
       <div class="container-fluid" style=""> 
        <div class="row"> 
         <div class="navbar-header"> 
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> 
           <span class="sr-only">Toggle navigation</span> Menu <i class="fa fa-bars"></i> 
          </button> 
          <a title "Matt's portfolio - go to homepage" style="margin-left:20px;margin-top:2px;" class="navbar-brand page-scroll" href="#"><img alt="Matt's portfolio logo" src="images/wm-logo.png" /></a> 
         </div> 

         <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
          <ul class="nav navbar-nav navbar-right" style="text-transform:uppercase;margin-right:20px;"> 
           <li class="menu-item-home"> 
            <a href="/home">Home</a> 
           </li> 
           <li class="dropdown"> 
            <a class="dropdown-toggle" data-toggle="dropdown" href="#">Projects <span class="caret"></span></a> 
            <ul class="dropdown-menu"> 
             <li><a href="/projects/graphics">Graphics</a></li> 
             <li><a href="/projects/websites">Websites</a></li> 
             <li><a href="/projects/others">Others</a></li> 
            </ul> 
           </li> 
           <li> 
            <a href="/about">About</a> 
           </li> 
           <li> 
            <a href="/contact">Contact</a> 
           </li> 
          </ul> 
         </div> 
        </div> 
       </div> 
      </nav> 
      <div class="container-fluid"> 
       <div class="row"> 
        <div class="col-lg-8 col-lg-offset-2 text-center"> 
         <h3 class="homepage-welcome">My name is Matt and this is my portfolio. </h3> 
        <h1 class="homepage-title">I make the Internet a better place.</h1> 
        <p>Passionate about People. Targeted on feelings and relationships. <br />Charmed by modern, frontend technologies.</p> 
       </div> 
      </div>   
     </div> 
    </div>  
</body> 

+0

你的代碼是不明確什麼問題都能在這裏找到工作的罰款。你可能不得不與你分享你的完整代碼。但是您可以嘗試更改JQuery版本。它可能會導致問題。 –

+0

編輯後的第一篇文章,請找到完整的代碼 –

回答

0

當你的代碼和疑問句並不多清楚,但我還是儘量根據一定的瞭解,這可能是你的疑問句的答回答你的問題。

案例1:

我想你不想隱藏在下拉菜單中,而在任何下拉菜單中的項目的點擊,因爲你已經寫了一些腳本這一點。 但您已添加'輸入'標籤,因爲在下拉菜單中沒有'輸入'標籤,所以不應該存在該標籤。 使用下面的腳本,

$('.dropdown-menu').click(function(e) { 

     e.stopPropagation(); 
    }); 

案例2:

如果你不想隱藏下拉按鈕的點擊下拉菜單,只是刪除了「數據切換」屬性爲它會在每次點擊按鈕時切換下拉菜單。

<li class="dropdown" id="dropDownBtn"> 
    <a class="dropdown-toggle" href="#">Projects <span class="caret"></span></a> 
    <ul class="dropdown-menu"> 
     <li><a href="/projects/graphics">Graphics</a></li> 
     <li><a href="/projects/websites">Websites</a></li> 
     <li><a href="/projects/others">Others</a></li> 
    </ul> 
</li> 

,並添加下面的腳本,

$("#dropdownBtn").on('click',function(){ 

      $(this).parent().addClass('open'); 
}); 

$(document).on('click',function(el){ 
    if(el.target.id != "dropdownBtn"){ //give some condition which suits your code 

       $('.dropdown').removeClass('open'); 
      } 
}); 

案例3: 正如你所提到的,你想通過點擊它崩潰的下拉列表。我不明白這一點,因爲這是bootstrap的正常行爲。 但是,如果你想所示的下拉只有一次,那麼你可以使用一個()方法的情況下,2

$("#dropdownBtn").one('click',function(){ 

      $(this).parent().addClass('open'); 
}); 

讓我知道,這種情況下適合你,而且,如果雙方的上述情況沒有按幫不了你,請清除問題,我會很感激你的幫助。 :)

0

$(document).ready(function() { 
 
      //$(".nav li").removeClass("active"); 
 
      $('.menu-item-home').addClass('active'); 
 
      //for demo 
 
      //$('#demo').addClass('active'); 
 
      //for sale 
 
      //$('#sale').addClass('active'); 
 
      $('.dropdown-menu input').click(function(e) { 
 
       e.stopPropagation(); 
 
      }); 
 
      }); 
 
     </script>
body { 
 
     background-image: linear-gradient(to bottom, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
 
     background-image: -moz-linear-gradient(top, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
 
     background-image: -o-linear-gradient(top, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
 
     background-image: -ms-linear-gradient(top, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
 
     background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(51, 51, 51, 0.8)), to(rgba(51, 51, 51, 0.8))), url("images/background-homepage.jpg"); 
 
     background-image: -webkit-linear-gradient(top, rgba(51, 51, 51, 0.8), rgba(51, 51, 51, 0.8)), url("images/background-homepage.jpg"); 
 
     filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/background-homepage.jpg', sizingMethod='scale'); 
 
     -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/background-homepage.jpg', sizingMethod='scale')"; 
 
     } 
 

 
     .navbar-default .navbar-nav>li>a 
 
     { 
 
     color:#e6e6e6; 
 
     font-size:11px; 
 
     padding-top:22px; 
 
     } 
 

 
     .navbar-default .navbar-nav>li>a:hover, .navbar-default .navbar-nav>.active>a, .navbar-default .navbar-nav>.active>a:focus, .navbar-default .navbar-nav>.active>a:hover { 
 
     color:#f9f9f9; 
 
     background:#2f2b2e; 
 
     border-bottom:2px solid #bfff51; 
 
     font-size:11px; 
 
     } 
 

 
     .navbar-default .navbar-nav>.open>a, .navbar-default .navbar-nav>.open>a:focus, .navbar-default .navbar-nav>.open>a:hover{ 
 
     color:#f9f9f9; 
 
     background:#2f2b2e; 
 
     border-bottom:2px solid #bfff51; 
 
     font-size:11px; 
 
     } 
 

 
     .dropdown-menu>li>a { 
 
     display: block; 
 
     padding: 3px 20px; 
 
     clear: both; 
 
     font-weight: 400; 
 
     line-height: 1.42857143; 
 
     color: #fefefe; 
 
     white-space: nowrap; 
 
     background:#4c4649; 
 
     font-size:11px; 
 
     } 
 

 
     .dropdown-menu { 
 
     position:absolute; 
 
     padding:0px; 
 
     border-radius:0px; 
 
     border:0px; 
 
     min-width:0; 
 
     background:red;font-size:11px; 
 

 
     } 
 

 
     .wrapper { 
 
     } 
 

 
     .homepage-welcome { 
 
     font-family:Lobster; 
 
     font-size:2em; 
 
     color:#fefefe;} 
 

 
     .homepage-title{ 
 
     font-family:'Open Sans'; 
 
     font-size:3em; 
 
     color:#fefefe;} 
 
     }
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 

 
<body> 
 
     <div class="wrapper"> 
 
      <nav class="navbar navbar-default" style="border-radius:0px;background:none;border:0px;"> 
 
       <div class="container-fluid" style=""> 
 
        <div class="row"> 
 
         <div class="navbar-header"> 
 
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> 
 
           <span class="sr-only">Toggle navigation</span> Menu <i class="fa fa-bars"></i> 
 
          </button> 
 
          <a title "Matt's portfolio - go to homepage" style="margin-left:20px;margin-top:2px;" class="navbar-brand page-scroll" href="#"><img alt="Matt's portfolio logo" src="images/wm-logo.png" /></a> 
 
         </div> 
 

 
         <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
 
          <ul class="nav navbar-nav navbar-right" style="text-transform:uppercase;margin-right:20px;"> 
 
           <li class="menu-item-home"> 
 
            <a href="/home">Home</a> 
 
           </li> 
 
           <li class="dropdown"> 
 
            <a class="dropdown-toggle" data-toggle="dropdown" href="#">Projects <span class="caret"></span></a> 
 
            <ul class="dropdown-menu"> 
 
             <li><a href="/projects/graphics">Graphics</a></li> 
 
             <li><a href="/projects/websites">Websites</a></li> 
 
             <li><a href="/projects/others">Others</a></li> 
 
            </ul> 
 
           </li> 
 
           <li> 
 
            <a href="/about">About</a> 
 
           </li> 
 
           <li> 
 
            <a href="/contact">Contact</a> 
 
           </li> 
 
          </ul> 
 
         </div> 
 
        </div> 
 
       </div> 
 
      </nav> 
 
      <div class="container-fluid"> 
 
       <div class="row"> 
 
        <div class="col-lg-8 col-lg-offset-2 text-center"> 
 
         <h3 class="homepage-welcome">My name is Matt and this is my portfolio. </h3> 
 
        <h1 class="homepage-title">I make the Internet a better place.</h1> 
 
        <p>Passionate about People. Targeted on feelings and relationships. <br />Charmed by modern, frontend technologies.</p> 
 
       </div> 
 
      </div>   
 
     </div> 
 
    </div>  
 
</body>

當你看到 檢查你的JQuery