开发者

jQuery click target selector

I have the following HTML in a page:

<a href="javascript:void(0)" class="move-next"><img src="/Content/images/small-next-arrow.png" height="16" width="16" alt="Next status arrow" title="Progress to next status"/></a></div>

and I have the following javascript that attempts to handle the click event:

function InitProgressionSelectors() {
$(".move-next").click(function() {
    moveNext(this);
});

$(".move-previous").click(function() {
    movePrevious(this);
});
}

function moveNext(target) {
    var sourceContainer = target.parent("td");
    var targetContainer = sourceContainer.next("td");
}

I'm obviously missing something, because "target" in the moveNext function is returning an HTMLAnchorElement, but when I try to wrap that, or access it somehow as a jQuery object, so I can try to get a handle to it's parent container, I get errors.

A reference to $(target) returns null. How do I get a reference to target as a jQuery object so I can work with it in th开发者_如何学Cat context? What am I missing?

Thanks.


You need pass the jquery object

$(".move-previous").click(function() {
    movePrevious($(this));
});


As I mentioned in the comments below, using $(target) didn't work. However, as can be seen in the comments on the original question above, the solution was to actually pass $(this) as the argument. Once I did that, target.parent resolved to the expected node, and worked.


bob can you try using dollor

$(target)

var sourceContainer = $(target).parent("td");


Another alternative is to pass the function in by reference:

$(".move-next").click(moveNext);

You can then use the selector as expected:

function moveNext() {
    var sourceContainer = $(this).parent("td");
    var targetContainer = sourceContainer.next("td");
}

Please note the lack of parentheses when passing the function to the click handler.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜