【问题标题】:Tabbed content with drop down menu带有下拉菜单的选项卡式内容
【发布时间】:2012-07-25 02:41:20
【问题描述】:

是否可以添加额外的 jQuery 来为这些选项卡添加下拉菜单?

这是添加了下拉菜单项的 HTML:

            <div class="tabs">

                <ul class="tabs-nav">

                    <li><a href="#tab1">Tab 1</a></li>
                    <li><a href="#tab2" >Tab 2</a></li>
                    <li><a href="#tab3" >Tab 3</a>
                          <ul><!-- added drop-down items -->
                               <li><a href="#son-of-tab3" >drop-down 1</a></li> 
                               <li><a href="#son-of-tab3" >drop-down 2</a></li> 
                             <li><a href="#son-of-tab3" >drop-down 3</a></li> 
                            </ul>
                     </li><!-- end tab 3-->

                </ul>

                <div class="tabs-content">

                    <div id="tab1" class="content">
                        <!-- tab1 content -->
                    </div>

                    <div id="tab2" class="content">
                        <!-- tab2 content -->
                    </div>

                    <div id="tab3" class="content">
                        <!-- tab3 content -->
                    </div>

                </div> <!-- /tabs-content -->

            </div> <!-- /tabs -->

使选项卡工作但需要额外的东西才能使下拉菜单工作的 jQuery?

            <script>
                $(document).ready(function() {

                    //add active class to the first li
                    $('.tabs-nav li:first').addClass('active');

                    //hide all content classes.
                    $('.content').hide();

                    //show only first div content
                    $('.content:first').show();

                    //add the click function
                    $('.tabs-nav li').click(function(){

                            //remove active class from previous li
                            $('.tabs-nav li').removeClass('active');

                            //add active class to the active li.
                            $(this).addClass('active');

                            //hide all content classes
                            $(".content").hide();

                            //find the href attribute of the active tab
                            var activeTab = $(this).find("a").attr("href");

                            //fade in the content of active tab
                            $(activeTab).fadeIn(1000);

                        return false;

                    });
                });
            </script>

如果需要,我可以发布 css

非常感谢,

【问题讨论】:

    标签: jquery tabs


    【解决方案1】:
    .tabs li ul{
      position:absolute;
        left:0;
      display:none;
    }
    
    $(function(){
      var parentH = $('.tabs-nav li ul').parent().height();
      $('.tabs-nav li ul').css('top', parentH);
      $('.tabs-nav li').hover(function(){
        $('ul', this).show();
      }, function(){
        $('ul', this).hide();
      });
    });
    

    Working jsFiddle

    【讨论】:

    • 谢谢 Ohgodwhy,我认为它现在唯一有效的是下拉菜单似乎卡在页面顶部? treadmagazine
    • @a_knife_a_fork 现在检查。
    • @a_knife_a_fork 你的顶级 LI 项目需要一个 position:relative; 应用于它们,以便 position:absolute; 不使用应用了位置的下一个最近元素的偏移量。这会给你想要的效果。苏..为你; .tabs-nav li{ position:relative //the rest of your styles}
    • 谢谢,我已经尝试过了,但它造成了各种破坏,而且我的新 jQery 不起作用...抱歉还有其他想法吗?
    • 此外,当我单击下拉菜单项时,它似乎会重新加载索引页面,而不是进入不同的页面。除了最后一点,一切正常吗?
    猜你喜欢
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多