JavaScript and querySelector

后端 未结 4 657
情话喂你
情话喂你 2021-01-25 03:55

I have this HTML:

Intermediate steps

\"intermediate
4条回答
  •  难免孤独
    2021-01-25 04:20

    Try using document.querySelectorAll which returns all of the possible results instead of just the first one. The error you're getting (Uncaught TypeError: Cannot set property 'src' of undefined) is because querySelector only returns the first element found, not an array (and elements can't be accessed like arrays).

    jQuery (the inspiration for querySelector and querySelectorAll) always allows you to access like an array ($('.staticStep img')[0] works), so this is probably where your confusion stems from.

    Quick JSFiddle example: http://jsfiddle.net/j8ZUJ/1/

提交回复
热议问题