$ (This) in jQuery is invalid in js original functions. _ jquery

Source: Internet
Author: User
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)
})

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.