简体   繁体   English

超级简单的jQuery选项卡 - 没有UI - 在页面加载时崩溃

[英]Super simple jQuery tabs - without UI - collapse on page load

I'd like all tabs to be collapsed on page load. 我希望所有标签在页面加载时折叠。 Right now the first one is open by default: 现在,第一个默认是打开的:

  $(document).ready(function($) {
    $('#forms').find('.forms-toggle').click(function(){

      //Expand or collapse this panel
      $(this).next().slideToggle('fast');

      //Hide the other panels
      $(".forms-content").not($(this).next()).slideUp('fast');

    });
  });

HTML and CSS are here: HTML和CSS在这里:

https://jsfiddle.net/re8x8cx3/ https://jsfiddle.net/re8x8cx3/

Place this inside dom ready, 把它放在dom准备就绪,

 $(".forms-content").hide();

Then the code will be, 然后代码将是,

$(document).ready(function($) {
  $(".forms-content").hide();
  $('#forms').find('.forms-toggle').click(function() {
    //Expand or collapse this panel
    $(this).next().slideToggle('fast');
    //Hide the other panels
    $(".forms-content").not($(this).next()).slideUp('fast');
  });
});

Fiddle 小提琴

Please also improve you JS: 还请你改进JS:

(function($){
    //store reusable global vars
    var $forms = $('#forms'),
        $formContents = $forms.find(".forms-content");

    //hide on load
    $formContents.hide();

    //attach only on handler to #forms instead of to every tab separately
    $forms.on('click', '.forms-toggle', function(event){
        event.preventDefault();

        //reuse this value (only on reference)
        var $thisContent = $(this).next(),

        //Expand or collapse this panel
        $thisContent.slideToggle('fast');

        //Hide the other panels
        $formContents.not($thisContent).slideUp('fast');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM