search

Home  >  Q&A  >  body text

javascript - Why does the push of the array superimpose the previous ones?


This is my click event. Why does it become new every time I add an element?

阿神阿神2828 days ago589

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-18 10:56:21

    ...Your arr is in the event and is a new arr every time. .

    reply
    0
  • ringa_lee

    ringa_lee2017-05-18 10:56:21

    Because you initialize arr to new data every time var arr = [];
    You should initialize arr on the click method

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-18 10:56:21

    Put var attr= []; outside the function and promote it to a global variable, and you should be fine.

    reply
    0
  • Cancelreply