1

我已经阅读了关于此的其他帖子,但是当单击选项卡时,我仍然无法让页面跳转到顶部。继承人的HTML

<ul class="tabs-nav">
    <li class="active"><a href="#tab1"><i class="icon-list-ul"></i> Tab 1</a></li>
    <li><a href="#tab2"><i class="icon-map-marker"></i> Tab 2</a></li>
</ul>

<!-- Tabs Content -->
<div class="tabs-container">
    <div class="tab-content" id="tab1">Stuff</div>
    <div class="tab-content" id="tab2">Stuff</div>
</div>

这是js...

(function() {

    var $tabsNav    = $('.tabs-nav'),
        $tabsNavLis = $tabsNav.children('li'),
        $tabContent = $('.tab-content');

    $tabsNav.each(function() {
        var $this = $(this);

        $this.next().children('.tab-content').stop(true,true).hide()
                                             .first().show();

        $this.children('li').first().addClass('active').stop(true,true).show();
    });

    $tabsNavLis.on('click', function(e) {
        var $this = $(this);

        $this.siblings().removeClass('active').end()
             .addClass('active');

        $this.parent().next().children('.tab-content').stop(true,true).hide()
                                                      .siblings( $this.find('a').attr('href') ).fadeIn();

        e.preventDefault();

    });

})();

现在我尝试添加

onclick="return false;"

到html中的标签href,但这无济于事。非常感谢任何帮助!谢谢!

4

1 回答 1

1

它因此而跃居榜首<a href="#tab1">

尝试<a href="javascript:void(0);"使用另一个属性来确定应该打开哪个 div,您可以添加任何以data-诸如data-id="#tab1 然后读取该属性开头的属性,

或者你可以重新考虑你的 HTML 是如何设置的,

如果您真的必须保持原样,那么您可以尝试

 var $tabsNav    = $('.tabs-nav'),
        $tabsNavLis = $tabsNav.children('a'),  //this line changed
        $tabContent = $('.tab-content');

然后:

 $tabsNavLis.on('click', function(e) {
        e.preventDefault();   //now this is <a> click , not <li> click
        var $this = $(this).parent();
        $this.siblings().removeClass('active').end()
             .addClass('active');
        $this.parent().next().children('.tab-content').stop(true,true).hide()
                                                      .siblings( $this.find('a').attr('href') ).fadeIn();


    });
于 2012-11-20T02:42:32.520 回答