开发者

jQuery chaining and toggle not running

开发者 https://www.devze.com 2022-12-08 18:37 出处:网络
If I run this code, var waitRow = $(this).parent().parent()开发者_JAVA技巧.next().get(0); $(waitRow).children(\'td:nth-child(2)\').html(\'some text\').toggle();

If I run this code,

var waitRow = $(this).parent().parent()开发者_JAVA技巧.next().get(0);
$(waitRow).children('td:nth-child(2)').html('some text').toggle();

toggle is not called.

If I instead write the following code it works. Why?

var waitRow = $(this).parent().parent().next().get(0);
$(waitRow).children('td:nth-child(2)').html('some text');
$(waitRow).toggle();


Because you are toggling the child element, not waitRow, I believe you could use .end() for this:

$(waitRow).children('td:nth-child(2)').html('some text').end().toggle();

To go back to the parent. Or use .parent() again.

Reference: http://docs.jquery.com/Traversing/end


The first one tries to toggle the first item in the children('td:nth-child(2)') wrapped set. The html() method returns the first matched item and not the whole collection.

The second one toggles the whole row.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号