开发者

jquery $this problem

see this code

$(function() {
    $('.do').deleteTable({
        parent: $(".do").parentsUntil("#did")

    });
});

its works but if i make it

$(function() {
$('.do').deleteTable({
parent: $(this).parentsUntil("#did")
});
});

no开发者_开发技巧t works; why? i replace $(".do") to $(this)


{ parent: $(this).parentsUntil("#did") } is an object literal, calculated before it is passed to deleteTable.

It is not a function called back from deleteTable with this set to each element with class do.

So this is the outer function() { ... }'s this: namely window.


This may help...

'this' demystified


"this" is probably the "window" object, which won't have parents.

I'm guessing you're thinking of a situation as follows:

$(function() {
    $('.do').click(function() {
    alert($(this).is('.do'));
    });
});

In the above case, when the function is run because of a click event, then "this" will be the $('.do') object you expect it to be. In your case however, the context of "this" is not $('.do')


When I'm in a similar situation, the first thing I do is set up a breakpoint and check (using firebug for example) if the $(this) is really what I'm expecting.

99% of the time that helps me understand what's going on.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜