[ACCEPTED]-Change font after createTextNode()-dom
You don't specify font on text nodes, you do so on 4 the parent element - in your case:
elem.style.fontSize = "20px";
If you don't 3 wish to change the font size for the entire 2 parent element, you can create a <span>
element 1 to wrap around the text node:
var span = document.createElement('span');
span.style.fontSize = "20px";
span.appendChild(s);
elem.appendChild(span);
createTextNode creates a Text node that has 14 only one method: splitText. setAttribute is a method 13 of the DOM Core that is implemented by the 12 Element interface (i.e. not text nodes).
Generally, you 11 should avoid setAttribute as it has numerous 10 quirks and setting the related DOM property 9 is faster and more reliable.
In any case, there 8 is no "fontSize" attribute specified 7 in HTML 4.01 for text nodes so you can't 6 expect browsers to implement it. Text nodes 5 inherit their style from their parent element, so 4 if you want to set the font size of some 3 text, wrap it in an element:
window.onload = function() {
var span = document.createElement('span');
// Set DOM property
span.style.fontSize = '200%';
span.appendChild(document.createTextNode('hey'));
// Add to document
document.body.appendChild(span);
};
But in general 2 you are better off to define the style in 1 a class and attach that to the span.
maybe you could use inline css. Never tried 1 this with a textnode though
setAttribute('style', 'font-size:-1;');
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.