I write some jQuery codes as below:
$('.button').each(function(){
$(this).click(function(){
console.log($(this));
do_sth();
});
});
var do_sth = function(){
console.log($(this));
}
I want the console.log results to be the same, but it works wrong… The first refers to a HTMLElement, but the second refers to DOMWindow…
How could I rewrite do_sth function to make them all refer to the HTMLElement?
Thanks.
You could do…
Alternatively, you could use jQuery’s
proxy()method.jsFiddle.