私はjQueryを使用しており、イベントコールバックとして機能する関数があるため、その関数では、「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
}
}