Javascript 错误消息“.innerHTML 不是函数”

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

Error message ".innerHTML is not a function"

javascripthtml

提问by Ender Che

I'm trying to fill the date automatically in a table header, but all I get is

我正在尝试在表头中自动填充日期,但我得到的只是

".innerHTML is not a function"

“.innerHTML 不是一个函数”

I've looked everywhere, and tried put my code at the top and bottom of the page, but nothing works. Help, please!

我到处寻找,并尝试将我的代码放在页面的顶部和底部,但没有任何效果。请帮忙!

window.onload = function() {
  var currentTime = new Date();
  var month = currentTime.getMonth() + 1;
  var day = currentTime.getDate();
  var year = currentTime.getFullYear();
  document.getElementById("dated").innerHTML(month + "/" + day + "/" + year);
};

回答by Timo

Well, as the error says, innerHTMLis not a function.

好吧,正如错误所说,innerHTML不是函数。

You can assign a value to the property, though:

但是,您可以为 property 分配一个值

document.getElementById("dated").innerHTML = month + "/" + day + "/" + year;

For more infos, have a look at the MDN docs.

有关更多信息,请查看MDN 文档

回答by BenAlabaster

innerHTML is a property... thus you need to use = not ()... it's not jQuery.

innerHTML 是一个属性...因此您需要使用 = not ()... 它不是 jQuery。

document.getElementById("dated").innerHTML = "blah"

回答by Kairat

document.getElementById("dated").innerHTML = month + "/" + day + "/" + year;