开发者

Append some HTML into the HEAD tag?

开发者 https://www.devze.com 2022-12-27 18:29 出处:网络
I want to add some style to head tag in html page using javascript. var h = document.getElementsByTagName(\'head\').item(0);

I want to add some style to head tag in html page using javascript.

var h = document.getElementsByTagName('head').item(0);
h.innerHTML += '<style>a{font-size:100px;}</style>';

But when I run this code in IE8 I see this 开发者_StackOverflow社区error message: Could not set the innerHTML property. Invalid target element for this operation.

Any ideas?


Create the style element with createElement:

var h = document.getElementsByTagName('head').item(0);
var s = document.createElement("style");
s.type = "text/css"; 
s.appendChild(document.createTextNode("a{font-size:100px;}"));
h.appendChild(s);


you can try:

let header = document.querySelector('head')
let newElement = document.createElement('p')
newElement.style.color = "lime"
newElement.style.fontSize = ".5rem"
header.append(newElement)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号