search

Home  >  Q&A  >  body text

How to update dom element selected by jquery?

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

女神的闺蜜爱上我女神的闺蜜爱上我2780 days ago834

reply all(3)I'll reply

  • 学习ing

    学习ing2017-07-05 10:45:06

    The only way is to assign the value to $ape again every time an element is added

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-07-05 10:45:06

    It 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.

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 10:45:06

    Either reassign the value, or when inserting a node, trigger an event and reassign the value. Anyway, it’s just reassignment

    reply
    0
  • Cancelreply