无法从集合中获取值(使用GetElementsByClassName)

问题描述 投票:0回答:1

我正在尝试显示一个从集合中获取其值的输出。

这是代码。对于显示两次的'schnapps',我设法得到我想要的输出。但对于'potato',我无法显示输出。

    <tr>
        <td>Schnapps Distillery</td>
        <td class='inputText schnapps'></td>
        <td>600 Farmers & Workers</td>
    </tr>
    <tr>
        <td colspan="3">
            <div class='inline'>
            <div class='inline potato'></div><img class='smallLogos'
                src='../images/potato.png' /> -->
            <div class='inline schnapps'></div><img class='smallLogos'
                src='../images/schnapps.png' />
            </div>
        </td>
    </tr>
schnappsElement = document.getElementsByClassName("schnapps");
potatoElement = document.getElementsByClassName("potato");

    //schnapps
    for (i = 0; schnappsElement[i] != null; i++) {
        schnappsElement[i].innerHTML = Math.ceil((Number(farmInput.value) + Number(workInput.value)) / 600);
    }

    potatoElement.innerHTML = schnappsElement[0].value;

JS中的两个命令都由我未包含的侦听器命令激活。该命令有效,因为我得到了Schnapps的正确输出。

我想要的是potatoElement拥有(并显示)与schnappsElement相同的值。

javascript html
1个回答
0
投票

qazxsw poi没有qazxsw poi。请改用qazxsw poi。

而且,schnappsElement[0]是一个数组 - 你需要将它用作.value()

.innerHTML

或者你可以简单地将它们包含在循环中:

potatoElement
© www.soinside.com 2019 - 2024. All rights reserved.