【发布时间】:2010-10-29 06:31:42
【问题描述】:
我正在使用 jQuery,并且我有一个用作事件回调的函数,因此在该函数中,“this”表示捕获事件的对象。但是,有一个实例我想从另一个函数显式调用该函数 - 在这种情况下,我如何设置函数中的“this”将等于什么?
例如:
function handleEvent(event) {
$(this).removeClass("sad").addClass("happy");
}
$("a.sad").click(handleEvent); // in this case, "this" is the anchor clicked
function differentEvent(event) {
$("input.sad").keydown(e) {
doSomeOtherProcessing();
handleEvent(e); // in this case, "this" will be the window object
// but I'd like to set it to be, say, the input in question
}
}
【问题讨论】:
标签: javascript jquery this jquery-events