javascript '无法设置未定义的属性“innerHTML”'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18779236/
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-10-27 13:12:36  来源:igfitidea点击:

'Cannot set property "innerHTML" of undefined'

javascripthtml

提问by Sean P. McMullen

I'm trying to write a pretty simple script to set an element's innerHTML to the time. However, Javascript keeps throwing a "Cannot set property 'innerHTML' of undefined" error. During debugging, I've simplified my script to the point that it runs right after element (a <span>) is coded, so I know it should have loaded already. I've also tried running this script as a <body onload=argument - same error. I can't tell what I'm doing wrong.

我正在尝试编写一个非常简单的脚本来将元素的innerHTML 设置为时间。但是,Javascript 不断抛出“无法设置未定义的属性‘innerHTML’”错误。在调试期间,我已将脚本简化为在元素 (a <span>) 编码后立即运行,因此我知道它应该已经加载了。我也试过将此脚本作为<body onload=参数运行- 同样的错误。我说不出我做错了什么。

<div style="position: fixed; right: 10px; top: 10px; width: auto; font-size: 16pt; border: 2px solid #000000;">
    <span id="clock">Loading...</span>
    <script type="application/x-javascript">
        function setClock(spanid){
            var d = new Date();
            document.getElementById[spanid].innerHTML = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds;
        }
        setClock("clock");
    </script>
</div>

Any help is appreciated!

任何帮助表示赞赏!

回答by Chris Hayes

You are not calling getElementById, you are attempting to index it. Since it is not an array and does not expose array-like behavior, the result is undefined. Replace your getElementById[spanId]with getElementById(spanId).

您不是在调用getElementById,而是试图对其进行索引。由于它不是数组并且不公开类似数组的行为,因此结果是undefined. 替换你getElementById[spanId]getElementById(spanId)

回答by Arun P Johny

[]instead of ()in document.getElementById(spanid). getElementByIdis function, it has to be invoked with ()and pass the parameter inside it.

[]而不是()document.getElementById(spanid)getElementById是函数,它必须被调用()并在其中传递参数。

Also missing ()after getSeconds()

也失踪()getSeconds()

<div style="position: fixed; right: 10px; top: 10px; width: auto; font-size: 16pt; border: 2px solid #000000;">
    <span id="clock">Loading...</span>
    <script type="application/x-javascript">
        function setClock(spanid){
            var d = new Date();
            document.getElementById(spanid).innerHTML = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
        }
        setClock("clock");
    </script>
</div>

Demo: Fiddle

演示:小提琴

回答by Unknown

change []to ():

更改[]()

function setClock(spanid){
            var d = new Date();
            document.getElementById(spanid).innerHTML = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds;
        }

回答by Thanos

And now that you have this fixxed add this:

现在你有了这个固定的添加这个:

 setInterval(function () {
     function setClock(spanid) {
         var d = new Date();
         document.getElementById(spanid).innerHTML = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
     }
     setClock("clock");
 }, 1);

And you have an actual clock :D

你有一个实际的时钟:D

EXAMPLE

例子