2015-06-03 69 views
0

我試圖從下拉菜單中選擇不同的選項時,要讓div內的內容發生更改。在工作之前,我在div內部有了選項卡,但他們看起來很醜,所以我放棄了他們的下拉菜單。我將在下拉菜單中引用地圖或網格,以供用戶在稍後執行時使用。下拉選擇無法更改div內容

工作代碼:http://codepen.io/MarkBond/pen/aOJLqm

HTML:

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> 
<div class="row"> 
    <div class="col-lg-12"> 
    <div class="ibox float-e-margins"> 
     <div class="ibox-title"> 
     <h5>Elements Map </h5> 
     <div class="ibox-tools"> 
      <button class="btn btn-default dropdown-toggle btn-xs btn-purple" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true"> 
      Views 
      <span class="caret"></span> 
      </button> 
      <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> 
      <li class="active" role="presentation"><a role="menuitem" tabindex="-1" href="#1">Map</a></li> 
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#2">List</a></li> 
      </ul> 
      <button type="button" class="btn btn-green btn-xs" data-toggle="modal" data-target="#myModal">Reset All</button> 
      <div class="modal fade" id="myModal" role="dialog"> 
      <div class="modal-dialog"> 
       <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal"> 
        <span class="glyphicon glyphicon-remove"></span> 
        </button> 
        <h4 class="modal-title">Element Map</h4> 
       </div> 
       <div class="modal-body"> 
        <p>Are you sure?</p> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
       </div> 
      </div> 
      </div> 
      <button type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#myModal"> 
      <span class="glyphicon glyphicon-fullscreen"></span> 
      </button> 
      <div class="modal fade" id="myModal" role="dialog"> 
      <div class="modal-dialog"> 
       <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal"> 
        <span class="glyphicon glyphicon-remove"></span> 
        </button> 
        <h4 class="modal-title">Element Map</h4> 
       </div> 
       <div class="modal-body"> 
        <p>Some text in the modal.</p> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
       </div> 
      </div> 
      </div> 
      <a class="collapse-link"> 
      <i class="fa fa-chevron-up"></i> 
      </a> 
     </div> 
     </div> 
     <div class="ibox-content"> 
     <div class="active" id="1"> 
      Lorem ipsum dolor sit amet, charetra varius quam sit amet vulputate. Quisque mauris augue, molestie tincidunt condimentum vitae, gravida a libero. Lorem ipsum dolor sit amet, charetra varius quam sit amet vulputate. Quisque mauris augue, molestie tincidunt 
      condimentum vitae, gravida a libero. Lorem ipsum dolor sit amet, charetra varius quam sit amet vulputate. Quisque mauris augue, molestie tincidunt condimentum vitae, gravida a libero. Lorem ipsum dolor sit amet, charetra varius quam sit amet 
      vulputate. Quisque mauris augue, molestie tincidunt condimentum vitae, gravida a libero. Lorem ipsum dolor sit amet, charetra varius quam sit amet vulputate. Quisque mauris augue, molestie tincidunt condimentum vitae, gravida a libero. Lorem 
      ipsum dolor sit amet, charetra varius quam sit amet vulputate. Quisque mauris augue, molestie tincidunt condimentum vitae, gravida a libero. 
     </div> 
     <div id="2"> 
      Secondo sed ac orci quis tortor imperdiet venenatis. Duis elementum auctor accumsan. Aliquam in felis sit amet augue. Secondo sed ac orci quis tortor imperdiet venenatis. Duis elementum auctor accumsan. Aliquam in felis sit amet augue. Secondo sed ac 
      orci quis tortor imperdiet venenatis. Duis elementum auctor accumsan. Aliquam in felis sit amet augue. Secondo sed ac orci quis tortor imperdiet venenatis. Duis elementum auctor accumsan. Aliquam in felis sit amet augue. Secondo sed ac orci 
      quis tortor imperdiet venenatis. Duis elementum auctor accumsan. Aliquam in felis sit amet augue. 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 

CSS:

.btn-green { 
    background-color:#33CC33; 
    color: #FFFFFF; 
} 

.btn-blue { 
    background-color:#3333FF; 
    color: #FFFFFF; 
} 

.btn-purple { 
    background-color:#CC00FF; 
    color: #FFFFFF; 
} 

.nav-dropdown-style { 
    font-weight: 600; 
    padding: 4px 12px; 
    margin: 14px 5px 5px 20px; 
    font-size: 14px; 
    float: right; 
    background: #1ab394; 
} 

.nav-dropdown-style:hover { 
    background: #199d82; 
} 
+1

你認爲寫js代碼來做到這一點嗎?這將是相對容易:)使用JavaScript的 – SzybkiSasza

+0

將使這容易很多。您可以搜索更改功能的下拉菜單並相應地隱藏和顯示div –

+0

我正在考慮使用js,但我並不真正熟悉它。 –

回答

0

如果你能夠編寫JavaScript,該解決方案將工作:

// Get all list options 
var listOptions = $('ul.dropdown-menu > li > a'); 

// Attach "click" event 
listOptions.click(function(ev) { 
    var href = $(this).attr('href'); 

    // Find divs with content 
    var divToShow = $(href); 
    var contentDivs = divToShow.parent().find('.content'); 

    contentDivs.removeClass('active'); 
    divToShow.addClass('active'); 
}); 

我佐助有點代碼。 Codepen可在此處獲得:http://codepen.io/anon/pen/XbMeoK。請注意,我也對CSS做了小的更改。

+0

謝謝它的工作原理,但我在頁面上的其餘內容中遇到問題。我在網頁上有更多的一個div,其中有3個有一個下拉,這應該是一樣的。他們中的每個人現在都是無內容的,當我從下拉菜單中選擇一個選項時,它將屏幕拖拽到它。 –

+0

注意使用「活動」類。只顯示「主動」類的內容,因此您必須添加「主動」到div,默認顯示內容(請仔細查看添加的CSS類)。我建議添加更多的描述性的類/ ID來區分每個組。還有一件事 - 對於這個例子來說,內容div的每個id應該是不同的。至於下拉 - UL和按鈕應該在一個「div」標籤中關閉,例如

SzybkiSasza

+0

我從標籤中發現了一些與之混淆的舊代碼,所以它們現在默認顯示。當我從下來選擇它仍然清除所有其他divs。我確實有自己的下拉列表,並列出它自己的div,但它似乎推動應該在旁邊的所有其他按鈕,在它下面。我使用所有的頁面代碼更新了codepen,以便看到即時消息的內容。 http://codepen.io/MarkBond/pen/aOJLqm –