Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript H1标题设置不正确_Javascript_Html - Fatal编程技术网

Javascript H1标题设置不正确

Javascript H1标题设置不正确,javascript,html,Javascript,Html,我正试图在我的网站(WordPress网站)上的h1标签中设置一些文本。我已经在页脚和页眉中测试了我的代码,这是在网页完全加载之前在网页上运行的最后一件事 <script> function setTitle() { alert('ok'); var a = document.getElementsByTagName("h1"); a.innerHTML = "yourTextHere"; } window.onload = setTitle; </s

我正试图在我的网站(WordPress网站)上的h1标签中设置一些文本。我已经在页脚和页眉中测试了我的代码,这是在网页完全加载之前在网页上运行的最后一件事

<script>
function setTitle() {
    alert('ok');
    var a = document.getElementsByTagName("h1");
    a.innerHTML = "yourTextHere";
}

window.onload = setTitle;
</script>

函数setTitle(){
警报(“正常”);
var a=document.getElementsByTagName(“h1”);
a、 innerHTML=“yourTextHere”;
}
window.onload=setTitle;
有没有人能指出我的代码有什么问题,或者是在WordPress中有什么与之冲突的地方


谢谢你,Luke。

getElementsByTagName
返回一个
HTMLCollection
(类似数组的对象)

使用

要获取数组的第一个元素,请尝试以下操作:

Don't use getElementsByTagName(). Instead use getElementsByID and give some ID to your h1 tag where you want to set.

And then Set it like

var a = document.getElementsByID("h1");
    a.innerHTML = "yourTextHere";
-


谢谢

在h1元素上设置一个ID并使用document.getElementById。如果要确保获得正确的元素,getElementsByTagName将返回一组元素,向文档中添加其他h1元素将导致问题。谢谢,但这不是一个真正相关的答案。
Don't use getElementsByTagName(). Instead use getElementsByID and give some ID to your h1 tag where you want to set.

And then Set it like

var a = document.getElementsByID("h1");
    a.innerHTML = "yourTextHere";