开发者

Using $(this) inside jQuery selector with context

开发者 https://www.devze.com 2023-02-07 11:00 出处:网络
my javascript looks like this: $(document).ready(function() { $(\"a:not(.noajax)\", $(\'#header, #content\')).live(\'click\', function(event) {

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

精彩评论

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