无法设置内部html的属性

时间:2017-10-05 12:54:53

标签: javascript html innerhtml

出于某种原因,每当我加载页面时,它告诉我它无法设置innerHTML的属性。我尝试了其他人都说的一切,但无济于事。我尝试添加一个window.onload,我尝试移动我的脚本标签,以便有时间加载DOM,但它仍然无效。我该怎么办?

<!-- The Canvas of which the buttons and labels will be placed on -->
    <canvas id="myCanvasUI" width="100" height="400" style="border: 1px solid #000000;"> </canvas>


<!-- Left and Right Arrows to modify the values of the Enzymes Variable -->
    <button class="buttonsEnzyme buttonsLeft" style="background: url(LeftArrow.png)" type="button" onclick="myEnzymesMinus()"></button>
        <p id="myEnzymesPara"> <b> ENZYMES </b> </p>
            <button class="buttonsEnzyme buttonsRight" style="background: url(RightArrow.png)" type="button" onclick="myEnzymesPlus()"></button>
                <p id="myEnzymesValue"></p>

<!-- Left and Right Arrows to modify the values of the Substrates Variable -->
    <button class="buttonsSubstrates buttonsLeft" style="background: url(LeftArrow.png)" type="button" onclick="mySubstratesMinus()"></button>
        <p id="mySubstratesPara"> <b> SUBSTRATES </b> </p>
            <button class="buttonsSubstrates buttonsRight" style="background: url(RightArrow.png)" type="button" onclick="mySubstratesPlus()"></button> 

<!-- Left and Right Arrows to modify the values of the Inhibitors Variable -->
    <button class="buttonsInhibitors buttonsLeft" style="background: url(LeftArrow.png)" type="button" onclick="myInhibitorsMinus()"></button>
        <p id="myInhibitorsPara"> <b> INHIBITORS </b> </p>
            <button class="buttonsInhibitors buttonsRight" style="background: url(RightArrow.png)" type="button" onclick="myInhibitorsPlus()"></button>

<!-- Left and Right Arrows to modify the values of the Temperature Variable -->
    <button class="buttonsTemperature buttonsLeft" style="background: url(LeftArrow.png)" type="button" onclick="myTemperatureMinus()"></button>
        <p id="myTemperaturePara"> <b> TEMPERATURE </b> </p>
            <button class="buttonsTemperature buttonsRight" style="background: url(RightArrow.png)" type="button" onclick="myTemperaturePlus()"></button>
                                        </div>

<!-- The Canvas of which the model will be placed on -->
    <canvas id="myCanvas" width="400" height="400" style="border: 1px solid #000000;"> </canvas>
        <canvas id="myModel" width="390" height="390" style="border: 1px solid #000000;"> </canvas>


<script>
var enzymes = 1;
var substrates = 20;
var inhibitors = 0;
var temperature = 25;
var container = 400;
var pH = 7;

function myEnzymesMinus() {
    enzymes -= 1;
    console.log(enzymes);
    }

function myEnzymesPlus() {
    enzymes += 1;
    console.log(enzymes);
    }

window.onload = function displayEnzyme() {
    document.getElementById(myEnzymesValue).innerHTML = enzymes;
    }

function mySubstratesMinus() {
    substrates -= 1;
    console.log(substrates);
    }

function mySubstratesPlus() {
    substrates += 1;
    console.log(substrates);
    }

</script>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

你只是忘了用引号括起来:

window.onload = function displayEnzyme() {
  document.getElementById('myEnzymesValue').innerHTML = enzymes;
}

顺便说一下,你不需要window.onload。将脚本放在底部就足够了。

希望它有所帮助。

答案 1 :(得分:0)

在引号中加myEnzymesValue,否则JS解释器会将其视为undefined值的未分类变量

&#13;
&#13;
<!-- The Canvas of which the buttons and labels will be placed on -->
    <canvas id="myCanvasUI" width="100" height="400" style="border: 1px solid #000000;"> </canvas>


<!-- Left and Right Arrows to modify the values of the Enzymes Variable -->
    <button class="buttonsEnzyme buttonsLeft" style="background: url(LeftArrow.png)" type="button" onclick="myEnzymesMinus()"></button>
        <p id="myEnzymesPara"> <b> ENZYMES </b> </p>
            <button class="buttonsEnzyme buttonsRight" style="background: url(RightArrow.png)" type="button" onclick="myEnzymesPlus()"></button>
                <p id="myEnzymesValue"></p>

<!-- Left and Right Arrows to modify the values of the Substrates Variable -->
    <button class="buttonsSubstrates buttonsLeft" style="background: url(LeftArrow.png)" type="button" onclick="mySubstratesMinus()"></button>
        <p id="mySubstratesPara"> <b> SUBSTRATES </b> </p>
            <button class="buttonsSubstrates buttonsRight" style="background: url(RightArrow.png)" type="button" onclick="mySubstratesPlus()"></button> 

<!-- Left and Right Arrows to modify the values of the Inhibitors Variable -->
    <button class="buttonsInhibitors buttonsLeft" style="background: url(LeftArrow.png)" type="button" onclick="myInhibitorsMinus()"></button>
        <p id="myInhibitorsPara"> <b> INHIBITORS </b> </p>
            <button class="buttonsInhibitors buttonsRight" style="background: url(RightArrow.png)" type="button" onclick="myInhibitorsPlus()"></button>

<!-- Left and Right Arrows to modify the values of the Temperature Variable -->
    <button class="buttonsTemperature buttonsLeft" style="background: url(LeftArrow.png)" type="button" onclick="myTemperatureMinus()"></button>
        <p id="myTemperaturePara"> <b> TEMPERATURE </b> </p>
            <button class="buttonsTemperature buttonsRight" style="background: url(RightArrow.png)" type="button" onclick="myTemperaturePlus()"></button>
                                        </div>

<!-- The Canvas of which the model will be placed on -->
    <canvas id="myCanvas" width="400" height="400" style="border: 1px solid #000000;"> </canvas>
        <canvas id="myModel" width="390" height="390" style="border: 1px solid #000000;"> </canvas>


<script>
var enzymes = 1;
var substrates = 20;
var inhibitors = 0;
var temperature = 25;
var container = 400;
var pH = 7;

function myEnzymesMinus() {
    enzymes -= 1;
    console.log(enzymes);
    }

function myEnzymesPlus() {
    enzymes += 1;
    console.log(enzymes);
    }

window.onload = function displayEnzyme() {
    document.getElementById('myEnzymesValue').innerHTML = enzymes;
    }

function mySubstratesMinus() {
    substrates -= 1;
    console.log(substrates);
    }

function mySubstratesPlus() {
    substrates += 1;
    console.log(substrates);
    }

</script>

</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您在document.getElementById函数中缺少双引号,这就是它无法获取该DOM元素的原因。 您必须将元素id括在双引号中。

document.getElementById("myEnzymesValue").innerHTML = enzymes;