Today, I encountered a depressing problem. As the title says, the original ecological function of js is parsed into undefined using $ (this) in jQuery. My example today is as follows,
The Code is as follows:
$ ("Ul li"). hover (function (){
SetTimeout (function (){
$ (This). addClass ("test ");
Alert ("0.3 s delayed! ") // Test
}, 300)
})
During running, this yellow part is not executed, and then I pop up $ (this) is originally undefined (actually I have guessed it );
Then I did not think about it (habits). I went directly to Baidu and did not find the answer. Then I thought about it myself, but I thought it was not reasonable, then I thought that I could not read it from the inside and could read it from the outside. Then I assigned $ (this) to a variable outside setTimeout. This will solve the problem. Haha, it's quite speechless. In fact, I am not trying to solve it here. I am talking about this idea. Do not go to Baidu Google to solve the problem first.
First, you have to find the answer.
The Code is as follows:
$ ("Ul li"). hover (function (){
Var oLi = $ (this );
SetTimeout (function (){
OLi. addClass ("test ");
Alert ("0.3 s delayed! ") // Test
}, 300)
})