[英]How to update an element using .textContent
I want to use a script using JavaScript that updates an element in the footer of a website.我想使用一个使用 JavaScript 的脚本来更新网站页脚中的元素。 However, the h3 element isn't getting updated.
但是,h3 元素没有更新。
I have tried textContent not updating HTML , but I wasn't able to find my solution there.我试过textContent 不更新 HTML ,但我无法在那里找到我的解决方案。
Here is my CodePen: https://codepen.io/martinlutherwinn/pen/NWPvRpZ这是我的 CodePen: https ://codepen.io/martinlutherwinn/pen/NWPvRpZ
This is my current code:这是我当前的代码:
<div class="Footer">
<h3></h3>
</div>
var copyrightedYear = (function() {
var header = document.getElementsByClassName("Footer")[0].textContent;
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var msg = "@" + currentYear + " Haitian Educators of Palm Beach.";
header = msg;
}());
You're setting a new value for header
, which is a copy of the text in the element.您正在为
header
设置一个新值,它是元素中文本的副本。 You need to assign to the property to change the text:您需要分配给属性以更改文本:
var copyrightedYear = (function() {
var header = document.getElementsByClassName("Footer")[0];
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var msg = "@" + currentYear + " Haitian Educators of Palm Beach.";
header.textContent = msg;
}());
Also copyrightedYear
will be undefined
unless you have return msg
.除非您有
return msg
否则copyrightedYear
将是undefined
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.