如何让 document.getElementsByTagName('').innerHTML 在 2 个标签之间制作文本?
How do I get document.getElementsByTagName('').innerHTML to make text between 2 tags?
我正在尝试使用 JavaScript 在多个网页上添加页脚,因此如果我想更改页脚,只需在一个地方进行更改。 PHP 在此服务器上不可用,服务器端插入 (SSI) 也不可用,但 Perl、Python 和 Tcl 可用。我一直在尝试 document.getElementsByTagName('footer').innerHTML = "text";
但它不生成文本。我从 dev.mozilla 复制了这段代码,它告诉我我有多少个标签:
var footer = document.getElementsByTagName('footer');
var num = footer.length;
console.log('There is ' + num + ' footer in this document');
所以,我不知道 innerHTML 脚本有什么问题。我也尝试了段落标签,在这两种情况下都得到了相同的结果。
我建议改用 textContent。为什么 here.
要查看其工作原理,请在使用 Whosebug 时将以下内容粘贴到浏览器控制台中,然后点击 enter。
document.querySelector('.site-footer').textContent = 'Custom footer content.'
注意:使用 querySelector
和 class 而不是 getElementByTagName
干杯!
在问这个问题之前,我已经搜索了 Python includes 没有任何运气,所以我停在那里,但是在问这个问题之后,我认为我应该搜索 Perl/Ruby includes。今天,我发现 I can use the Perl use function, so I could study that and try to implement it although I am completely new to Perl. Ruby also appears capable,也许更多。我也没有使用 Ruby 的经验,但也许我应该从那里开始。
我刚刚发现 getElementsByTagName() 会产生一个数组,所以我必须使用 [0]:
来引用页脚的索引
var footerTags = document.getElementsByTagName('footer');
footerTags[0].innerHTML = "test";
我正在尝试使用 JavaScript 在多个网页上添加页脚,因此如果我想更改页脚,只需在一个地方进行更改。 PHP 在此服务器上不可用,服务器端插入 (SSI) 也不可用,但 Perl、Python 和 Tcl 可用。我一直在尝试 document.getElementsByTagName('footer').innerHTML = "text";
但它不生成文本。我从 dev.mozilla 复制了这段代码,它告诉我我有多少个标签:
var footer = document.getElementsByTagName('footer');
var num = footer.length;
console.log('There is ' + num + ' footer in this document');
所以,我不知道 innerHTML 脚本有什么问题。我也尝试了段落标签,在这两种情况下都得到了相同的结果。
我建议改用 textContent。为什么 here.
要查看其工作原理,请在使用 Whosebug 时将以下内容粘贴到浏览器控制台中,然后点击 enter。
document.querySelector('.site-footer').textContent = 'Custom footer content.'
注意:使用 querySelector
和 class 而不是 getElementByTagName
干杯!
在问这个问题之前,我已经搜索了 Python includes 没有任何运气,所以我停在那里,但是在问这个问题之后,我认为我应该搜索 Perl/Ruby includes。今天,我发现 I can use the Perl use function, so I could study that and try to implement it although I am completely new to Perl. Ruby also appears capable,也许更多。我也没有使用 Ruby 的经验,但也许我应该从那里开始。
我刚刚发现 getElementsByTagName() 会产生一个数组,所以我必须使用 [0]:
来引用页脚的索引var footerTags = document.getElementsByTagName('footer');
footerTags[0].innerHTML = "test";