Javascript 无法设置内部html的属性

Javascript 无法设置内部html的属性,javascript,html,innerhtml,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" st

出于某种原因,每当我加载页面时,它都会告诉我无法设置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>

抑制剂

温度

var=1; var=20; var=0; var温度=25; var=400; var-pH=7; 功能myelzymesminus(){ 酶-=1; console.log(酶); } 函数myesplus(){ 酶+=1; console.log(酶); } window.onload=函数displayEzyme(){ getElementById(myEzymesValue).innerHTML=酶; } 函数mySubstratesMinus(){ 基质-=1; 控制台日志(基板); } 函数mySubstratePlus(){ 基质+=1; 控制台日志(基板); }
您只是忘了在选择器中加引号:

window.onload = function displayEnzyme() {
  document.getElementById('myEnzymesValue').innerHTML = enzymes;
}
您不需要
窗口。顺便说一下,onload
。将脚本放在底部就足够了


希望有帮助。

在引号中取
MyEzymesValue
,否则JS解释器会将其视为
undefined
值的未计算变量

抑制剂

温度

var=1; var=20; var=0; var温度=25; var=400; var-pH=7; 功能myelzymesminus(){ 酶-=1; console.log(酶); } 函数myesplus(){ 酶+=1; console.log(酶); } window.onload=函数displayEzyme(){ getElementById('myEzymesValue')。innerHTML=酶; } 函数mySubstratesMinus(){ 基质-=1; 控制台日志(基板); } 函数mySubstratePlus(){ 基质+=1; 控制台日志(基板); }
document.getElementById函数中缺少双引号,因此无法获取该DOM元素。 必须用双引号将元素id括起来


document.getElementById(“myEzymesValue”).innerHTML=酶

您从未设置
myEzymesValue
,因此您的元素将是未定义的-它没有innerHTML属性,这就是您无法设置它的原因。如果它不是变量,则需要在其周围加引号:
document.getElementById('myEzymesValue')。innerHTML=enzymes您应该在引号中使用MyEzymesValue,如js中的“MyEzymesValue”