Chrome console handling jQuery $(this) bug?
It is kind of strange.
<table>
<tr>
<td>feng</td>
</tr>
</table>
jQuery code:
$('table tr').click(function(e){
console.log($(this)==null); //returns False
});开发者_Python百科
The Chrome console outputs False
, which is expected.
However, when I toggle a break point in the function and trigger the event, I found that ,if I type $(this)
in the Chrome console, it returns null
The this
"variable" isn't really a variable. I don't think Chrome establishes a value for this
to match that in the current execution context; it'd be pretty hard to do that, frankly.
If you want to be able to debug such a function, just make sure you put the this
value into a regular old local variable:
var saveThis = this;
Then, $(saveThis)
should work for you.
I usually find it handy to save "this" when I plan to pass it to another function.
var $this = $(this);
精彩评论