js native querySelector writing method

ask:

Is this writing method const winningBall = document.querySelector('winningBall') correct?

answer:

This way of writing is incorrect because the `document.querySelector()` method requires passing in a string as a parameter to specify the element to be selected. If the parameters passed in are not in the correct format, an error will be thrown.

In this case, `winningBall` is a class name and should be preceded by a dot (.) to indicate that it is a CSS class name selector. Therefore, the correct way to write it should be:

```javascript
const winningBall = document.querySelector('.winningBall');
```

In this way, the first matching element can be selected based on the class name of `winningBall` and assigned to the `winningBall` variable.

Supongo que te gusta

Origin blog.csdn.net/kuang_nu/article/details/130972790
Recomendado
Clasificación