views:

37

answers:

1

Hello, I'm loading a file into a div with load() function, but after I load the data - the styles for it doesn't want to work.

For example:

index.html:

 $("a ").click(

     function(e)
     { 
     e.preventDefault(); 

        $("#Display").load('file.html');    
        $("#Display").show();
 });  

file.html:

<h1 id="title">Item number #1</h1>
<p id="content">Lorem ipsum like text...</p>

style.css:

#title {
     color: red;
}

#content {
     color: green;
}

After I click "a" link, the content is loaded to #Display div, then it's perfectly displayed, but the title header is not red and content paragraph is not green.

I believe that's a default behavior because when site loads at first there are no such elements and I'm loading them dynamically with jQuery. Is there a way to lazy load them and refresh style-sheet in the background somehow? Or any other tricks to help me?

Thanks a lot!

A: 

Is the stylesheet referenced in the head element of the file being loaded? If so, it won't be loaded by load because load silently filters everything but the content of the body element. If you include the stylesheet in the document from which the script runs and into which the other document will be loaded, it should work just fine.

<head>
<link rel="stylesheet" type="text/css" href="styles.css" media="screen />
...
<script type="text/javascript" src="...jquery.js"></script>
<script type="text/javascript">
$(function() {
    $("a").click( function(e) { 
        $("#Display").load('file.html');    
        $("#Display").show();
        return false;
    });
});
</script>
</head>
<body>
<p><a href="#">Load Content</a></p>
<div id="Display"></div>
</body>
tvanfosson
The stylesheet is embedded in main index.html file where jQuery functions are. So I guess everything is correct?There's no head section in loaded document since it's not a whole document, but only a few paragraphs of text (something like PHPs including).
aldona
It should work then. Is is possible that other styles are overriding them? Have you checked with Firebug to see what rules are being applied after the document is loaded?
tvanfosson