Want to add “addEventListener” on multiple elements with same class

后端 未结 3 1500
野的像风
野的像风 2020-12-21 03:38

I\'d like to perform the logic in the \"onClick\" through the event listener in jS but it only seems to run once? I have the class in all four but I can\'t figure out why it

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-21 04:15

    Look at the documentation for querySelector:

    querySelector() returns the first Element within the document that matches the specified selector, or group of selectors.

    If you want to match more than one element, you'll need to use querySelectorAll and, because it doesn't return a single element loop over the result.

    Alternatively, you could use event delegation.

提交回复
热议问题