简体   繁体   English

函数仅适用于一个div?

[英]Function only works on one div?

我被困住了……我似乎无法在第一个细分以下的div上触发一个函数。……有什么建议吗?

Perhaps you are using something like this: 也许您使用的是这样的:

document.getElementById("myDiv")

With HTML like this: 使用这样的HTML:

<div id="myDiv">Hello World</div>
<div id="myDiv">Hello Again</div>

In which case, you can't use the same ID for two div tags and the divs after your first one won't be affected by your JavaScript. 在这种情况下,您不能对两个div标签使用相同的ID,并且第一个div之后的div不会受到JavaScript的影响。

You can use: 您可以使用:

document.getElementsByTagName("div")

To get a collection of all your divs, you could then test them to see if you want to do something with them, for example by giving them all the same class. 为了获得所有div的集合,您可以测试它们以查看是否要对它们进行某些操作,例如,给它们全部相同的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM