JavaScript innerHTML 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10826756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:05:31  来源:igfitidea点击:

JavaScript innerHTML not working

javascript

提问by NullPoiиteя

Here is a very simple program and the output should be sand JavaScript but I am only getting a.

这是一个非常简单的程序,输出应该是s和 JavaScript,但我只得到a.

<html>
    <head>
        <title></title>
        <script type="text/javascript">
          document.getElementById("ma").innerHTML="JavaScript";
        </script>
    </head>
    <body>
        <h1 id="ma">s</h1>
    </body>
</html>

回答by Sampson

The element doesn't exist at the time you're attempting to set a value. You need to call this after the <h1>has been added to the DOM.

在您尝试设置值时该元素不存在。您需要<h1>在添加到 DOM后调用它。

You can either move this <script>tag down further, or add your logic to a function which ought to be called when the document has been loaded:

您可以将此<script>标签进一步向下移动,也可以将您的逻辑添加到应该在加载文档时调用的函数:

window.onload = function() {
    /* Add your logic here */
}

Demo: http://jsfiddle.net/Lr2Hm/

演示:http: //jsfiddle.net/Lr2Hm/

回答by Someth Victory

You have 2 choices:

您有 2 个选择:

window.onload = function() {
  //your script here
}

or

或者

<body>
    <h1 id="ma">s</h1>
    <script type='text/javascript'>
        //your script here
    </script>
</body>

回答by u283863

You have to wait until the DOM has loaded.

您必须等到 DOM 加载完毕。

window.onload = function(){
    document.getElementById("ma").innerHTML="JavaScript";
}

回答by Myriam Rouve

Note that anything you write with element.innerHTML="anything" will replace the actual content of your element. If you want to add more content you have to store everything into a cumulative variable, the actual content of your element too, in order to keep it, then parse this variable into innerHTML when its content is full.

请注意,您使用 element.innerHTML="anything" 编写的任何内容将替换您的 element 的实际内容。如果您想添加更多内容,您必须将所有内容都存储到一个累积变量中,元素的实际内容也是如此,以便保留它,然后在其内容已满时将此变量解析为 innerHTML。

<script type=text/javascript">
var MyFullVar = element.innerHTML + "my new content I want to add";
element.innerHTML = MyFullVar;
</script>

回答by DeadlyDerpYT

You could also do this instead...

你也可以这样做......

<script type=text/javascript">
    window.onload = function () {
        var element = document.getElementById('element').innerHTML;
        var content = ".........";
        element.innerHTML += content.toString();
    }
    </script>

According to w3schoolsand basic JavaScript rules this works. I have also tried this before.

根据w3schools和基本的 JavaScript 规则,这是有效的。我以前也试过这个。

This takes the basis on the onload function to auto run the code at startup. It also takes advantage of the JS Assignment Operators to quickly and efficiently execute the code.

这需要基于 onload 函数在启动时自动运行代码。它还利用 JS 赋值运算符来快速有效地执行代码。

回答by kaeiris

The options provided did not work for me. document.querySelectorworked for me!

提供的选项对我不起作用。 document.querySelector为我工作!

for example:

例如:

document.querySelector("#ma").innerHTML = "HELLO";