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

高!

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

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

我肯定会错过一些非常基本的东西,或者只是不太了解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(); }); } 

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

有任何想法吗?

问题是这样的:

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

当你以某种方式调用一个函数时, this成了window 。 而是这样做:

 $(this).click(attachHideMyChildren); 

此外,您正在添加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; }); 

应该做的。