【问题标题】:Javascript/jQuery - Hide on clickJavascript/jQuery - 点击隐藏
【发布时间】:2017-02-28 09:30:40
【问题描述】:

我是 JS 的新手,所以请温柔一点。

我有一组标签,我正在移动时将它们做成手风琴。它工作得很好,但是在移动设备中,我希望再次点击它们时关闭“标签”。单击时我尝试添加.hide(),但没有任何反应。我究竟做错了什么?标记看起来像这样:

<ul class="tabs">
  <li class="active" rel="tab1">Tab 1</li>
  <li rel="tab2">Tab 2</li>
  <li rel="tab3">Tab 3</li>
  <li rel="tab4">Tab 4</li>
</ul>

<div class="tab_container">
  <h3 class="d_active tab_drawer_heading" rel="tab1">Tab 1</h3>
  <div id="tab1" class="tab_content">
      <h2>Tab 1 content</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac metus augue.</p>
  </div>
  <!-- #tab1 -->

  <h3 class="tab_drawer_heading" rel="tab2">Tab 2</h3>
  <div id="tab2" class="tab_content">
      <h2>Tab 2 content</h2>
        <p>Nunc dui velit, scelerisque eu placerat volutpat, dapibus eu nisi. Vivamus eleifend vestibulum odio non vulputate.</p>
  </div>
  <!-- #tab2 -->

  <h3 class="tab_drawer_heading" rel="tab3">Tab 3</h3>
  <div id="tab3" class="tab_content">
      <h2>Tab 3 content</h2>
        <p>Nulla eleifend felis vitae velit tristique imperdiet. Etiam nec imperdiet elit. Pellentesque sem lorem, scelerisque sed facilisis sed, vestibulum sit amet eros.</p>
  </div>
  <!-- #tab3 -->

  <h3 class="tab_drawer_heading" rel="tab4">Tab 4</h3>
  <div id="tab4" class="tab_content">
      <h2>Tab 4 content</h2>
        <p>Integer ultrices lacus sit amet lorem viverra consequat. Vivamus lacinia interdum sapien non faucibus. Maecenas bibendum, lectus at ultrices viverra, elit magna egestas magna, a adipiscing mauris justo nec eros.</p>
  </div>
  <!-- #tab4 --> 

</div>
<!-- .tab_container -->

JS 看起来像这样:

 // tabbed content
$(".tab_content").hide();
$(".tab_content:first").show();

  /* if in tab mode */
$("ul.tabs li").click(function() {

  $(".tab_content").hide();
  var activeTab = $(this).attr("rel"); 
  $("#"+activeTab).fadeIn();        

  $("ul.tabs li").removeClass("active");
  $(this).addClass("active");

  $(".tab_drawer_heading").removeClass("d_active");
  $(".tab_drawer_heading[rel^='"+activeTab+"']").addClass("d_active");

});
/* if in drawer mode */
$(".tab_drawer_heading").click(function() {

  $(".tab_content").hide();
  var d_activeTab = $(this).attr("rel"); 
  $("#"+d_activeTab).fadeIn();                         
  $(".tab_drawer_heading").removeClass("d_active");
  $(this).addClass("d_active");

  $("ul.tabs li").removeClass("active");
  $("ul.tabs li[rel^='"+d_activeTab+"']").addClass("active");

if(".tab_drawer_heading".hasClass('d_active')){ */This is what I tried to make it hide*/
  $(".tab_drawer_heading").click(function() {
    $("this.tab_content").hide();
})};
});


/* Extra class "tab_last" 
   to add border to right side
   of last tab */
$('ul.tabs li').last().addClass("tab_last");

我使用了代码here 并根据我的需要和内容进行了调整。

【问题讨论】:

    标签: javascript jquery tabs accordion


    【解决方案1】:

    您可以检查单击的选项卡是否为活动选项卡,如果是,则在您关闭活动选项卡后返回。

    // Lets detect if it is a phone, you can use your code here
    var isMobile = false;
    /* From http://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device-in-jquery */
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
        isMobile = true;
    }
    
    
    // Lets fix your code here
    /* if in drawer mode */
    $(".tab_drawer_heading").click(function() {
    
        $(".tab_content").hide();
        var d_activeTab = $(this).attr("rel");
    
        // If `this` is active
        if(isMobile && $("ul.tabs li[rel^='"+d_activeTab+"']").hasClass("active")){
            $(".tab_drawer_heading").removeClass("d_active");
            $("ul.tabs li").removeClass("active");
            return;
        }
    
        $("#"+d_activeTab).fadeIn();                         
        $(".tab_drawer_heading").removeClass("d_active");
        $(this).addClass("d_active");
    
        $("ul.tabs li").removeClass("active");
        $("ul.tabs li[rel^='"+d_activeTab+"']").addClass("active");
    });
    

    另外请记住,我们通常只附加一次事件,因此任何elementX.click 都不应该在您多次调用的函数中。

    【讨论】:

      【解决方案2】:

      如果我正确理解您的问题。如果您想关闭移动设备的“tab_content”部分,只需添加以下功能。

          $(".tab_content").click(function() {
      
             if ($(window).width() <= 700){   
              // do something for mobile devices
             $(this).hide();
          }
      
          });
      

      【讨论】:

        猜你喜欢
        • 2010-11-08
        • 1970-01-01
        • 2014-06-09
        • 1970-01-01
        • 2011-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-15
        相关资源
        最近更新 更多