The value of $ (this) changes with confirmation

I am wondering why $(this) does not work as I expect? In the code below, nothing happens when you click "Delete Image". If you comment out the confirmation expression, the background will turn green when you click Delete Image. Do you know why this is so? It seems that $(this) pointing to something else due to a confirmation statement. Thanks in advance!

 <a href="#" class='thumbnail the-one delete-file'><i class="icon-remove"></i>Remove Image</a> $('.the-one').click(function(){ if(confirm("What do you say?")) { return true;} else {return false;} $(this).css("background", "green"); }); 
+4
source share
2 answers

You come back before installing css so that the line does not execute. Try:

 $('.the-one').click(function(){ if (confirm("What do you say?")) { $(this).css("background", "green"); return true; } else { return false; } }); 
+5
source

Because it has a return in front of it. Everything after return will fail.

+5
source

Source: https://habr.com/ru/post/1481493/


All Articles