-1
    <div id="menu" class="menu">
<ul id="items"  class="items">
    <li class="item" data-load="link to a page has contents >">cars</li>
    <li class="item" data-load="link to a page has contents >">games</li>
    <li class="item" data-load="link to a page has contents >">sport</li>
    <li class="item" data-load="link to a page has contents >">news</li>
    <li class="item" data-load="link to a page has contents >">cats</li>
    <li class="item" data-load="link to a page has contents >">toys</li>
</ul>
    
<div id="load_items" class="load_items_here">
    <!--items will load here by js code-->  
    </div>
</div>


 <script type="text/javascript">    
 //load categories inside the sidebar div          
        $(() => {
  $('.items').on('click','.item',function() {   
       var page = $(this).attr('data-load');
     $('#load_items').load(page);
  });
});  
     </script>

the contents that come from data-load="link to a page has contents >" and loaded in div id="load_items" not get css codes . i dont know why !!

but the div menu and the ul + li get css .

how to fix this problem ?

3
  • The CSS might be external to the page you're loading, and the CSS URL might be relative to the domain/host from which you're loading the page. Of course I cannot tell, because you don't give us an example of what you're trying to load. In general loading a page like that works badly. You could try a <iframe>. Commented Oct 28, 2024 at 8:06
  • So what is the CSS you hope will be applied? Commented Oct 28, 2024 at 13:00
  • my question was answered . thanks Commented Oct 28, 2024 at 15:41

1 Answer 1

-2

Try this one, i have used my own css to check and verify this.

$(document).ready(() => {
    $('.items').on('click', '.item', function() {
        var page = $(this).attr('data-load');
        $('#load_items').load(page, function() {
            // Optional: Force a reflow if necessary
            $(this).hide().show(0);
        });
    });
});
body {
    font-family: Arial, sans-serif;
}

.menu {
    width: 200px;
    float: left;
    background: #f0f0f0;
    padding: 10px;
}

.items {
    list-style-type: none;
    padding: 0;
}

.item {
    padding: 10px;
    cursor: pointer;
}

.item:hover {
    background-color: #ddd;
}

.load_items_here {
    margin-left: 220px; /* Space for the menu */
    padding: 10px;
    border: 1px solid #ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div id="menu" class="menu">
    <ul id="items" class="items">
        <li class="item" data-load="content/cars.html">Cars</li>
        <li class="item" data-load="content/games.html">Games</li>
        <li class="item" data-load="content/sport.html">Sport</li>
        <li class="item" data-load="content/news.html">News</li>
        <li class="item" data-load="content/cats.html">Cats</li>
        <li class="item" data-load="content/toys.html">Toys</li>
    </ul>

    <div id="load_items" class="load_items_here">
        <!-- Items will load here by JS code -->
    </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

works 100% . but i have a question : i replaced this code $(this).hide().show(0); with document.querySelector('#load_items #container_content').style.border='3px solid #F81115'; how to add more css fore this code ? background-color border-radius ... etc (the #container_content is an element that has the issue that not get css. but by your code now it can get css .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.