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
Mary-Kate OlsenOriginal
2024-12-04 15:54:11901browse

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;

jQuery Solution:

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

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!

Statement:
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