javascript - cant assign null value to a variable in jquery -


Here once I slide the 'IMG' variable into a slide, and click on the 'Click Image Gallery' IMG value is 'undefined', why is it?

  $ (". ImageGallery"). Click (function () {console.log (img + "first"); var img = $ (this) .attr ("src"); / / Image source console.log (img + "second"); var num = $ (This) .attr ("alt"); // image name $ ('# popup'). Slidedown ("600", function () {$ (This) .html ("& lt; div & gt; & Lt; p & gt; "+ num +" & lt; / p & gt; & lt; / div & gt; & lt; img src = "+ img +" & gt; "); $ (" #popup AddClass ("popupNameBg"); $ ('popupNameBg');) $ ('# popup'); addClass ("popupName"); $ ("popup name"); addClass ("popupImg"); $ ("# Popup div "). (Function () {$ ('# popup') SlideUp (" 500 "); img = null; console.log (img +" third "); return false;}}}})  

You set it This is going to be undefined because of how it is

  console.log (IMG + "before"); & lt; - Before reading the value, read the variable, this var img = $ (this) .attr ("src") will be undefined; & lt; - / set Variable value console.log (img + "second"); & Lt; - The new value of  

Your code is actually:

  var img; Console.log (img + "before"); IMG = $ (this) .attr ("src"); Console.log (img + "second");  

Since every new click you are in a new "scope" and can not define a new variable. So when you click the pop up, it is setting the IMG in an empty space, the problem is that you do not use that variable anymore.

On each click you get a new popup Clicking is bound, which means that you have lots of click events.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -