jquery将$(this)传递给其他函数 [英] jquery passing $(this) to other functions

查看:140
本文介绍了jquery将$(this)传递给其他函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

高!

我想要做的是以下几点:我有一个连接到位于偶数行表中的链接的onclick表。每一行都是隐藏的。点击该链接时,会显示奇数行并将数据加载到该行中。工作正常

现在我想要做的是每当完成该过程时,我想为该链接附加一个新的点击功能,使该行再次隐藏。有点像切换,但随着一些更只是显示/隐藏功能。我试图用下面的代码做到这一点,但无法让它工作。

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

  $(document).ready(function(){

//版本图标
$(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();
});
$ b $(this).click(function(){
attachHideMyChildren() ;
});

return false;
});
});

函数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();
});
返回false;
}

函数attachHideMyChildren()
{
$(this).parent()。parent()。next(tr.version)。hide );
$(this).click(function(){
attachShowMyChildren();
});
}

它打开表格行,插入数据,但不附加函数再次关闭该行。我怎么能得到这个发生?



任何想法?

解决方案

问题是这样的: ();

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

当您以这种方式调用函数时,这个变成窗口。这样做:

  $(this).click(attachHideMyChildren); 

另外,您要添加 click()处理程序没有删除旧的。



这就是说,这样做有一个更简单的方法。



<$ ()函数(){
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();
}
返回false;
});

应该这样做。


High!

What i want to do is the following: i have a table with a onclick attached to a link that resides in the table of an even row. every odd row is hidden. on a click on that link, the odd row is shown and data is loaded into that row. Works fine

Now what i want to do is that whenever that process is done, i want to attach a new click function to that link that makes the row hidden again. Kind of like toggle, but then with some more then just show/hide functionality. I tried to do that with the following code, but cant get it to work.

I surely miss something very basic or just do not know jquery well enough (which is very well possible, because i just got started for a few weeks ago).

$(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();
    });
}   

It opens the table row, inserts the data but then doesn't attach the function to close the row again. How can i get this to happen?

Any ideas?

解决方案

The problem is this:

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

When you call a function that way this becomes window. Instead do this:

$(this).click(attachHideMyChildren);

Also, you're adding click() handlers without removing the old ones.

That being said, there is an easier way of doing this.

$("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;
});

should about do it.

这篇关于jquery将$(this)传递给其他函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆