【发布时间】:2021-05-10 20:13:46
【问题描述】:
我正在尝试将图像附加到 8 个容器之一,而无需编写我想要的代码。我希望它能够查看哪些容器是空的并附加到这些容器中。我编写了一些我认为可以工作的东西,但它只附加到第 8 个容器,即使其他 7 个是空的。似乎还有更好的方法来做到这一点
if (beatcontainer1.firstChild){
currentbeat = beatcontainer2}
if (beatcontainer2.firstChild){
currentbeat = beatcontainer3}
if (beatcontainer3.firstChild){
currentbeat = beatcontainer4}
if (beatcontainer4.firstChild){
currentbeat = beatcontainer5}
if (beatcontainer5.firstChild){
currentbeat = beatcontainer6}
if (beatcontainer6.firstChild){
currentbeat = beatcontainer7}
if (beatcontainer7.firstChild){
currentbeat = beatcontainer8}
currentbeat.appendChild(qnotec)
【问题讨论】:
-
如果有实际的代码需要批评/测试,这将更容易提供帮助,但如果您在上面的 IF 语句中使用 ELSE 可能会有所帮助。
-
您可能会考虑一个循环遍历每个容器并附加到空容器。我同意将relevant HTML code 包含在内会有所帮助,以便其他人可以重现该问题。
标签: javascript append containers