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 error message: Could not set the innerHTML property. Invalid target element for this operation.
Any ideas?
Add the opening and closing <head> tags inside of the <html> tags. Next, add two additional lines of HTML code inside the <head> tags like this: Note that you have nested a variety of HTML elements inside one another. The <title> and <meta> elements are nested inside the <head> element, and the <head> element is nested inside the <html> element.
HTML <head> Tag 1 Definition and Usage. The <head> element is a container for metadata (data about data) and is placed between the <html> tag and the <body> tag. 2 Browser Support 3 Global Attributes. The <head> tag also supports the Global Attributes in HTML. 4 More Examples 5 Related Pages 6 Default CSS Settings
Next, add two additional lines of HTML code inside the <head> tags like this: Note that you have nested a variety of HTML elements inside one another. The <title> and <meta> elements are nested inside the <head> element, and the <head> element is nested inside the <html> element. We will nest elements frequently as the tutorial proceeds.
Example. A simple HTML document, with a <title> tag inside the head section: <!DOCTYPE html>. <html lang="en">. <head>. <title> Title of the document </title>. </head>. <body>. <h1> This is a heading </h1>.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With