2015-11-19 3 views
0

Я не так хорошо разбираюсь в JavaScript и могу использовать некоторую помощь. Я хочу, чтобы мой код содержал все на одной странице, но мой код JavaScript меняет содержимое моих DIVs одним нажатием кнопки. Я сделал это и работаю.Вкладки для содержания JavaScript

Однако, я хочу, чтобы первый DIV был открытым по умолчанию, когда открывается страница. И когда отдельный div открыт, я хочу, чтобы соответствующая кнопка меняла цвета фона (через CSS). Это та часть, на которую я застрял. Помогите?

Мой код до сих пор:

<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 
</head> 

<body> 
<div class="why-container"> 


    <div class="icon-nav"> 

     <a class="link" href="#tab" data-link="first"><img src="img1.png" class="blue-icons"></a> 
     <a class="link" href="#tab" data-link="second"><img src="img2.png" class="blue-icons"></a> 
     <a class="link" href="#tab" data-link="third"><img src="img3.png" class="blue-icons"></a> 
     <a class="link" href="#tab" data-link="fourth"><img src="img4.png" class="blue-icons"></a> 

    </div> 

    <div class="why-content"> 

     <div class="textWord_tab" data-link="first"> 
      <div class="content-box"> 
       <div class="content-box-header">Content Tab 1</div> 
       <p class="content-box-text">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
      </div> 

     </div> 


     <div class="textWord_tab" data-link="second"> 
      <div class="content-box"> 
       <div class="content-box-header">Content Tab 2</div> 
       <p class="content-box-text">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
      </div>   
     </div> 


     <div class="textWord_tab" data-link="third"> 
      <div class="content-box"> 
       <div class="content-box-header">Content Tab 3</div> 
       <p class="content-box-text">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
      </div>   
     </div> 


     <div class="textWord_tab" data-link="fourth"> 
      <div class="content-box"> 
       <div class="content-box-header">Content Tab 4</div> 
       <p class="content-box-text">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
      </div>   
     </div> 


    </div> 

</div> 

<script type="text/javascript"> 
    $('.textWord_tab').hide(); 

    $('.link').click(function() { 
     $('.textWord_tab').hide(); 
     $('.textWord_tab[data-link=' + $(this).data('link') + ']').fadeIn({ 
      width: '200px' 
     }, 300); 
    }); 
</script> 

</body> 

ответ

0

работать только с хэштегов и HREF, определить свои кнопки как этот

<div class="icon-nav"> 

    <a class="link" href="#first"><img src="img1.png" class="blue-icons"></a> 
    <a class="link" href="#second"><img src="img2.png" class="blue-icons"></a> 
    <a class="link" href="#third"><img src="img3.png" class="blue-icons"></a> 
    <a class="link" href="#fourth"><img src="img4.png" class="blue-icons"></a> 

</div> 

и вы JS, как это (класс «выбран» должны быть определены проектировать выбранные кнопки):

//select all tabs and hide 
var tabs = $('.textWord_tab').hide(); 
var buttons = $('.link'); 

function showTab(key) { 
    //select showing tab 
    var tabToShow = tabs.filter('[data-link=' + key + ']'); 

    //check if tab is hidden 
    if(tabToShow.is(':hidden')) { 
     buttons.removeClass('selected') 
      .filter('[href=#' + key + ']').addClass('selected'); 
     tabs.hide(); 
     tabToShow.stop().fadeIn({ 
      width: '200px' 
     }, 300); 
     return true; 
    } 

} 
//show the first tab 
if(!window.location.hash || !showTab(window.location.hash)) { 
    showTab(buttons.filter(':first').attr('href').substr(1)); 
} 

buttons.click(function() { 
    showTab($(this).attr('href').substr(1)); 
}); 

вы можете видеть, хэштегом будет обновляться со значением канальном , при перезагрузке последняя открытая вкладка будет открыта вместо первой

+0

Фантастическая! Это добавило функциональность, которую я хотел. Не могли бы вы узнать, как я могу делать эффекты вкладок при изменении фона для кнопок? – ticklishoctopus

+0

@ticklishoctopus смотреть мое редактирование, просто более плавное решение (добавление также выбранного класса) – silly

+0

Да! Это сделал трюк! Большое спасибо за Вашу помощь. Ваши комментарии были действительно полезными. – ticklishoctopus

Смежные вопросы