I will try to expand my problem:
I have a link:
<a href="#" onClick="Display(); return false;">Display</a>
I have some styles:
<style type="text/css">
p {
background-color: #AFA99B;
}
</style>
And the folowing js code:
function printf(MyHtml){
document.write(MyHtml);
}
function Display(){
printf("<p>some html code<p>");
//...
}
So when the user click the link, I must add some html content to the body & the new added elements must use the styles defined.
That’s all.
That’s the problem was talking about in my comment. You are calling
document.write(MyHtml)after the document was fully loaded.It will completely replace the document (see the documentation). Inspect it, you will see that there is an empty
head(also see this and this example).Use other methods to manipulate the DOM.