Home > Web Front-end > CSS Tutorial > How Can I Dynamically Apply CSS to AJAX-Loaded Content with jQuery?

How Can I Dynamically Apply CSS to AJAX-Loaded Content with jQuery?

Barbara Streisand
Release: 2024-12-02 12:04:15
Original
843 people have browsed it

How Can I Dynamically Apply CSS to AJAX-Loaded Content with jQuery?

Dynamically Applying Inline or External CSS with jQuery

When loading HTML content via AJAX using jQuery, you may encounter issues with applying CSS to the loaded content. This is because dynamically added CSS is not automatically evaluated by the browser.

Options for Evaluation:

To resolve this issue, you can use several methods to evaluate CSS dynamically:

1. Using AJAX to Load CSS:

$.get(myStylesLocation, function(css) {
  $('<style type="text/css"></style>').html(css).appendTo("head");
});   
Copy after login

2. Using Dynamically-Created :

$('<link rel="stylesheet" type="text/css" href="'+myStylesLocation+'" >').appendTo("head");
Copy after login

3. Using Dynamically-Created