简体   繁体   中英

H1 title not setting correcly

I'm trying to set some text into h1 tag on my website (WordPress Site). I've tested my code in the footer and in the header and it is the last thing to run on the webpage before it is fully loaded.

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

window.onload = setTitle;
</script>

Is anyone able to point out what is wrong with my code or is it something else conflicting with it in WordPress.

Thanks, Luke.

getElementsByTagName returns an HTMLCollection (an array-like object).

Use

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

to get the first element of the array.

Try this:

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

-

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM