Home > Web Front-end > CSS Tutorial > How to Properly Remove or Replace a Stylesheet with JavaScript/jQuery?

How to Properly Remove or Replace a Stylesheet with JavaScript/jQuery?

Mary-Kate Olsen
Release: 2024-12-04 15:54:11
Original
898 people have browsed it

How to Properly Remove or Replace a Stylesheet with JavaScript/jQuery?

Removing or Replacing a stylesheet (a ) with JavaScript/jQuery

Question:

How do I remove or replace a stylesheet (a ) using JavaScript or jQuery? I have tried using $('link[title="mystyle"]').remove(), but the styles are still applied to the current page. Is there another way?

Answer:

To remove a stylesheet across various browsers, including Internet Explorer, you need to disable it rather than removing it. JavaScript/jQuery can achieve this by modifying the disabled property of the stylesheet.

Cross-Browser Solution:

document.styleSheets[0].disabled = true;
Copy after login

jQuery Solution:

$('link[title=mystyle]')[0].disabled = true;
Copy after login

This approach ensures that the CSS styles are no longer applied to the current page, even if the element itself is removed.

The above is the detailed content of How to Properly Remove or Replace a Stylesheet with JavaScript/jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template