0

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

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

window.onload = setTitle;
</script>

有没有人能指出我的代码有什么问题,或者它在 WordPress 中是否有其他冲突。

谢谢,卢克。

4

2 回答 2

1

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

采用

var a = document.getElementsByTagName("h1")[0];

获取数组的第一个元素。

于 2013-10-17T10:55:51.270 回答
0

试试这个:

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";

-

谢谢

于 2013-10-17T11:07:23.700 回答