首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >制表符之间的切换不适用于JQuery

问制表符之间的切换不适用于JQuery
EN

Stack Overflow用户
提问于 2014-06-11 10:45:43
回答 4查看 109关注 0票数 1

所以,我有一个简单的菜单,在haml,它是用来标签的。

代码语言:javascript
复制
    .monthly_tab#selected
      Monthly
    .yearly_tab#notselected
      Yearly

这是在制表符之间切换的JQuery代码。它不能完全正确地工作。我可以从.monthly_tab切换到.yearly_tab,但不能返回。

代码语言:javascript
复制
$(document).ready(function() {
    $("#notselected").click(function(){
        if ($("#notselected").hasClass("yearly_tab")){
            $(".yearly_tab#notselected").removeAttr("id")
            $(".yearly_tab").attr("id", "selected")
            $(".monthly_tab#selected").removeAttr("id")
            $(".monthly_tab").attr("id", "notselected")
            $(".prices.monthly").hide()
            $(".prices.yearly").show()
        }else if ($("#notselected").hasClass("monthly_tab")){
            $(".monthly_tab#notselected").removeAttr("id")
            $(".monthly_tab").attr("id", "selected")
            $(".yearly_tab#selected").removeAttr("id")
            $(".yearly_tab").attr("id", "notselected")
            $(".prices.yearly").hide()
            $(".prices.monthly").show()
        }
    });

});

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-06-11 11:06:52

您可以使用反转ids和类来完成这一任务:

代码语言:javascript
复制
#monthly_tab.selected.tab
  Monthly
#yearly_tab.notselected.tab
  Yearly

为单击函数添加了额外的类tab,因此脚本将非常短:

代码语言:javascript
复制
$(".tab").click(function () {
    $(".selected").addClass("notselected").removeClass("selected");
    $(this).removeClass("notselected").addClass("selected"); 
});

示例

票数 1
EN

Stack Overflow用户

发布于 2014-06-11 11:09:45

click事件仅绑定到匹配"#notselected“的元素一次--它不会自动绑定到任何匹配该id的新元素。

切换时,您需要解除绑定/绑定单击事件

代码语言:javascript
复制
function rebind () {
    $("#notselected").unbind('click').click(handle);
}

function handle() {

    if ($("#notselected").hasClass("yearly_tab")){

        $(".yearly_tab#notselected").removeAttr("id")
        $(".yearly_tab").attr("id", "selected")
        $(".monthly_tab#selected").removeAttr("id")
        $(".monthly_tab").attr("id", "notselected")
        $(".prices.monthly").hide()
        $(".prices.yearly").show()

    } else if ($("#notselected").hasClass("monthly_tab")){

        $(".monthly_tab#notselected").removeAttr("id")
        $(".monthly_tab").attr("id", "selected")
        $(".yearly_tab#selected").removeAttr("id")
        $(".yearly_tab").attr("id", "notselected");
        $(".prices.yearly").hide()
        $(".prices.monthly").show()

    }

    rebind();
}

$(document).ready(function() {
    $("#notselected").unbind('click').click(handle);
});
票数 1
EN

Stack Overflow用户

发布于 2014-06-11 11:01:42

标签代码示例尝试如下-

代码语言:javascript
复制
$(document).ready(function() {    

   $('#tabs li a:not(:first)').addClass('inactive');
   $('.container').hide();
   $('.container:first').show();

   $('#tabs li a').click(function(){
       var t = $(this).attr('id');
       if($(this).hasClass('inactive')){  
          $('#tabs li a').addClass('inactive');           
          $(this).removeClass('inactive');

          $('.container').hide();
           $('#'+ t + 'C').fadeIn('slow');
        }
   });

});

请参考此小提琴。

参考文献

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24160948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档