在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 我想問這種的 tab 切換有沒有簡化的空間?

我想問這種的 tab 切換有沒有簡化的空間?

$('.desc-btn').click(function(e) {
  $('.desc-btn').removeClass('productview_tab_inactive').addClass('productview_tab_active');
  $('.info-btn, .note-btn').removeClass('productview_tab_active').addClass('productview_tab_inactive');
  $('.desc-content').show('fast');
  $('.info-content').hide('fast');
  $('.note-content').hide('fast');
});
$('.info-btn').click(function(e) {
  $('.info-btn').removeClass('productview_tab_inactive').addClass('productview_tab_active');
  $('.desc-btn, .note-btn').removeClass('productview_tab_active').addClass('productview_tab_inactive');
  $('.info-content').show('fast');
  $('.desc-content').hide('fast');
  $('.note-content').hide('fast');
});
$('.note-btn').click(function(e) {
  $('.note-btn').removeClass('productview_tab_inactive').addClass('productview_tab_active');
  $('.desc-btn, .info-btn').removeClass('productview_tab_active').addClass('productview_tab_inactive');
  $('.note-content').show('fast');
  $('.info-content').hide('fast');
  $('.desc-content').hide('fast');
});

這是我寫的
但我發(fā)現(xiàn)似乎可以再簡化?但我已經(jīng)想不到
想問線上的大神的簡化寫法。

補充
果然有,感謝各位大神,太神啦

回答
編輯回答
葬憶

好熱鬧。我也試試,題目沒有給出html結構,所以答題時我就自由發(fā)揮了。
以下是完整的css,js,html DEMO

<style type="text/css">
    .tab-btn {
        display: inline-block;
        padding: 5px 10px;
    }

    .tab-content {
        display: none;  /*關鍵*/
        height: 0;      /*關鍵*/
    }

    .tab-btn.active{
        color: white;
        background: black;
    }


    .tab-content.active{
        display: block;  /*關鍵*/
        height: auto;    /*關鍵*/
    }
</style>

<div class="tabs">
    <div class="tab-btn active">DESC</div>
    <div class="tab-btn">INFO</div>
    <div class="tab-btn">NOTE</div>
</div>
<div class="contents">
    <div class="tab-content active">DESC-Content</div>
    <div class="tab-content">INFO-Content</div>
    <div class="tab-content">NOTE-Content</div>
</div>

<script type="text/javascript">
    $('.tab-btn').each(function(index) {
        $(this).data('index', index);
    });
    $('.tab-btn').click(function() {
        var index = $(this).data('index');
        $(this).addClass('active').siblings().removeClass('active');
        $('.tab-content:eq(' + index +')').show('fast').siblings().hide('fast');
    });
</script>
2017年10月21日 03:42
編輯回答
凹凸曼

當然有, 把你 html 源碼貼一下

2018年9月18日 22:34
編輯回答
荒城

類似于這樣,使用 $(this).自行修改

$('.btn').click(function(e) {
  $(this).removeClass('productview_tab_inactive').addClass('productview_tab_active').show();
  $(this).siblings().removeClass('productview_tab_active').addClass('productview_tab_inactive').hide();
});
2017年5月10日 01:19
編輯回答
掛念你
$('.tabList').on('click', '.tabToggle', function(event) {
  event.preventDefault();

  const $this = $(this);
  const $tabList = $this.parents('.tabList');
  $tabList.find('.tabToggle').removeClass('active');
  $this.addClass('active');

  const targetId = $this.attr('href');
  const $target = $(targetId);
  const $tapContent = $target.parents('.tabContent');
  $tapContent.find('.tabPanel').removeClass('active');
  $target.addClass('active');
});

使用方式

<div class="tabList">
    <a class="desc-btn tabToggle" href="#desc">desc</a>
    <a class="info-btn tabToggle" href="#info">info</a>
    <a class="note-btn tabToggle" href="#note">note</a>
</div>

<div class="tabContent">
    <div id="desc" class="tabPanel" >
    <div id="info" class="tabPanel" >
    <div id="note" class="tabPanel" >
</div>

可以任意增加 tab 而不需要再次修改 js.

2017年11月20日 16:36