jquery将$(this)传递给其他函数

hal*_*erd 2 jquery function this toggle

高!

我想要做的是以下内容:我有一个带有onclick的表连接到位于偶数行的表中的链接.每个奇数行都是隐藏的.单击该链接时,将显示奇数行,并将数据加载到该行中.工作良好

现在我想要做的是,无论何时完成该过程,我想将新的点击功能附加到该链接,使该行再次隐藏.有点像切换,但后来有一些更多然后只是显示/隐藏功能.我尝试使用以下代码执行此操作,但无法使其工作.

我肯定会错过一些非常基本的东西,或者只是不太了解jquery(这很可能,因为我刚开始几周前).

$(document).ready(function(){

    // The version icons
    $("a.version").click(function () {
        var sLink = $(this).attr("href");
        var nexttr = $(this).parent().parent().next("tr.version");
        var nexttrtd = nexttr.find("td:first");
        $.get(sLink, function(sData){
            nexttrtd.html(sData);
            nexttr.show();
        });

        $(this).click(function(){
            attachHideMyChildren();
        });

        return false;
    });
});

function attachShowMyChildren()
{
    var sLink = $(this).attr("href");
    var nexttr = $(this).parent().parent().next("tr.version");
    var nexttrtd = nexttr.find("td:first");
    $.get(sLink, function(sData){
        nexttrtd.html(sData);
        nexttr.show();
    });
    $(this).click(function(){
        attachHideMyChildren();
    });
    return false;
}

function attachHideMyChildren()
{
    $(this).parent().parent().next("tr.version").hide();
    $(this).click(function(){
        attachShowMyChildren();
    });
}   
Run Code Online (Sandbox Code Playgroud)

它打开表行,插入数据但是不附加函数再次关闭行.我怎么能让这件事发生?

有任何想法吗?

cle*_*tus 5

问题是这样的:

$(this).click(function(){
  attachHideMyChildren();
});
Run Code Online (Sandbox Code Playgroud)

当你以某种方式调用一个函数thiswindow.而是这样做:

$(this).click(attachHideMyChildren);
Run Code Online (Sandbox Code Playgroud)

此外,您正在添加click()处理程序而不删除旧处理程序.

话虽如此,有一种更简单的方法可以做到这一点.

$("a.version").click(function() {
  var next = $(this).closest("tr").next();
  if (next.is(":hidden")) {
    $.get($(this).attr("href"), function(sData) {
      next.find("td:first").html(sData);
      next.show();
    });
  } else {
    next.hide();
  }
  return false;
});
Run Code Online (Sandbox Code Playgroud)

应该做的.