For example, there is <p class="ape">Element 1</p>
I use jquery to cache it into a variable:
var $ape = $('.ape');
Then I added a new one<p class="ape">Element 2</p>
What method should $ape use to refresh itself at this time? How to enable it to select two apes at the same time?
I don’t want to have to $('.ape') every time I call it, I just want to call the cached variable $ape
The only way is to assign the value to
$ape
again every time an element is addedIt is recommended to use
$('.ape')
directly. Don't write code exactly according to your own wishes, try to adapt your code to the grammatical features. The code written this way is more valuable.Either reassign the value, or when inserting a node, trigger an event and reassign the value. Anyway, it’s just reassignment