Javascript 选项卡 - 事件类

标签 javascript jquery css tabs

我正在使用以下 js,它非常适合在单击 5 个选项卡之一时隐藏和显示内容。效果很好,但我的问题是,我如何调整代码,以便在当前显示选项卡的内容时,该选项卡具有事件类。悬停类效果很好,除了事件类之外的其他所有内容也是如此。非常感谢任何帮助:

$(window).ready(function() {
    $('#infotab').click(function() {
        $(document).find('.tabcontent').hide();
        $('.infotabcontent').show();
        $(document).find('.top-nav2-menu li').removeClass('tabactive');
        $('#infotab').addClass('tabactive');
        $('#reviewtab').removeClass('tabactivelast');
    });
    $('#findingtab').click(function() {
        $(document).find('.tabcontent').hide();
        $('.findingtabcontent').show();
        $(document).find('.top-nav2-menu li').removeClass('tabactive');
        $('#findingtab').addClass('tabactive');
        $('#reviewtab').removeClass('tabactivelast');
        document.getElementById('frame1').contentDocument.location.reload(true);
    });
    $('#streetviewtab').click(function() {
        $(document).find('.tabcontent').hide();
        $('.streetviewtabcontent').show();
        $(document).find('.top-nav2-menu li').removeClass('tabactive');
        $('#streetviewtab').addClass('tabactive');
        $('#reviewtab').removeClass('tabactivelast');
        document.getElementById('frame2').contentDocument.location.reload(true);
    });
    $('#videotab').click(function() {
        $(document).find('.tabcontent').hide();
        $('.videotabcontent').show();
        $(document).find('.top-nav2-menu li').removeClass('tabactive');
        $('#videotab').addClass('tabactive');
        $('#reviewtab').removeClass('tabactivelast');
    });
    $('#reviewtab').click(function() {
        $(document).find('.tabcontent').hide();
        $('.reviewtabcontent').show();
        $(document).find('.top-nav2-menu li').removeClass('tabactive');
        $('#reviewtab').addClass('tabactivelast');
    });
});

最佳答案

你的代码很痛苦......

  • $(window).ready(function() { 应该是 $(function() { 这是 $(document).ready(function(){

  • 的简写
  • 在您的 HTML 中,为所有 id="***tab" 元素分配一个类 class="tab"

  • 缓存您的元素集合 $('.tabcontent')$('.top-nav2-menu li')

    <
  • 使用$(this) 选择器

这就是您所需要的:

$(function(){  // DOM is now ready

    // Cache your selectors
    var $tabCont  = $(".tabcontent"),
        $topNavLi = $(".top-nav-menu li"),
        $tabRev   = $('#reviewtab');

    $('.tab').click(function() {

        var myId = this.id;

        if(myId=="findingtab"){
             $('#frame1')[0].contentDocument.location.reload(true);
        }
        if(myId=="streetviewtab"){
             $('#frame2')[0].contentDocument.location.reload(true);
        }        

        $tabCont.hide();
        $("."+ myId +"content").show();

        $(this).addClass('tabactive').siblings().removeClass('tabactive');

        $tabRev.removeClass('tabactivelast');
        if(myId=="reviewtab"){
           $(this).addClass('tabactivelast');
        }

    });

});

关于Javascript 选项卡 - 事件类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21080395/

相关文章:

html - 如何更改事件链接的颜色?

jquery - NivoSlider NivoControl Nav IE7 不显示

javascript - 如何禁用鼠标中键拖动选择?

javascript - 通过 cid 而不是 id 在 Backbone.js 集合中查找模型

javascript - 使用 W3C 剪贴板 API 将文本从 extjs htmleditor 组件复制到剪贴板

javascript - 防止 JavaScript 启动的滚动

javascript - 如何实现悬停在图像上的效果?

javascript - 我可以在不破坏新浏览器的情况下使用 "one fits all"polyfill 集合吗?

javascript - 在代码中使用 .attr ("disabled"时,Jquery Mobile 多选列表禁用选项不起作用

html - 如何显示在其 CSS 属性中添加了 'fixed' 和 'inline-block' 的 DIV