开发者

Using $(this) inside jQuery selector with context

my javascript looks like this:

$(document).ready(function() {
    $("a:not(.noajax)", $('#header, #content')).live('click', function(event) {
        ajaxCall($(this).attr("href"));
        event.preventDefault();
    });
)};

The $(this) selector doesn't seem to get the 'a' object, it works fine however without a selector context. What am I doing wrong here?


Looks like jQuery converts select开发者_如何转开发ors with context into :

$(context).find(selector)

So I think that my $(this) refers to the context rather than the selctor. Any ideas?


The context should be an element or a string, not a jQuery object. Furthermore, since ids are unique, you only need one context here.

$("a:not(.noajax)", '#header').live('click', function(event) {
    ajaxCall($(this).attr("href"));
    event.preventDefault();
});

See a live example.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜