setAttribute不确定,为什么?

发布于 2025-02-03 19:03:08 字数 325 浏览 1 评论 0原文

这是我的代码:

[在html]````

``` [in css]``` .myclass { width:50%; }```

[在JS] function candlyWidth(){让mynodes = document.getElementsByClassName('。myClass')); mynodes.setAttribute('width','100%'); }

然后,运行此功能时,它显示: Unturect TypeError:无法阅读未定义的属性,当阅读“ setAttribute”时

会发生什么以及如何纠正? 谢谢

This is my code follow:

[in html]```

```
[in css]```
.myclass {
width:50%;
}```

[in js]function changewidth() { let mynodes = document.getElementsByClassName('.myclass'); mynodes.setAttribute('width','100%'); }

then, when this function be run, it display:
Uncaught TypeError: Cannot read properties of undefined, when reading 'setAttribute'

what happen and how to correct ?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狠疯拽 2025-02-10 19:03:09

该错误说mynodes变量是未定义的,含义JavaScript无法在任何地方找到它,因为它不存在。

删除其中的点(。)

document.getElementsByClassName('.myclass')

应该是

document.getElementsByClassName('myclass')

The error says that the myNodes variable is undefined meaning JavaScript cant find it anywhere because it doesn't exist.

Remove the dot(.) in

document.getElementsByClassName('.myclass')

It should be

document.getElementsByClassName('myclass')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文