简体   繁体   中英

Alerting return value shows correct string whereas storing as variable returns undefined

This returns the expected value:

alert($('#previous a').attr("href"));

This returns undefined :

var a = $('#previous a').attr("href");
alert(a)

I could not figure out why. Can someone help me :)

Your code is working fine.Try below.

 var a = $('#previous a').attr("href"); alert(a) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="previous"> <a href="www.google.com">Google</a> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM