【问题标题】:Issue with 'this' class jquery'this'类jquery的问题
【发布时间】:2013-11-28 11:03:27
【问题描述】:

我正在尝试将this 插入到我的函数中,这样我就可以拥有多个刻度。

$('.bt').click(function(e){
    var num = $(this).attr('href');
    $('.rat').hide(); 
    $('.' + num).show(); 
    e.preventDefault();
});

JSFIDDLE

【问题讨论】:

  • 这里是演示:jsfiddle.net/kyPS7
  • 你想使用this调用哪个函数?
  • 但它对我有用
  • 您在哪个浏览器中查看?
  • 问题是,如果我点击第一个比例,另一个得到相同,我想彼此独立

标签: jquery class this


【解决方案1】:

要仅编辑一个比例,您可以使用parent()children() 函数,而不是“全局”选择元素。这是你需要的(JSFiddle):

$('.bt').click(function(e){
    var num = $(this).attr('href');
    $(this).parent().children('.rat').hide(); 
    $(this).parent().children('.' + num).show(); 
    e.preventDefault();
});

【讨论】:

  • +1 用于理解问题.. 我根本无法理解
【解决方案2】:

目前,您正在选择类.rat 的所有元素。您只想选择当前元素的兄弟姐妹。看看jQuery's siblings 并尝试这样的事情:

$('.bt').click(function(e){
    var num = $(this).attr('href');
    $(this).siblings('.rat').hide(); 
    $(this).siblings('.' + num).show(); 
    e.preventDefault();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2021-02-27
    • 2012-02-26
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 2014-01-04
    相关资源
    最近更新 更多