I got a line on the top of the page,
<link rel="stylesheet" href="style/style.css">
How do I remove it by jQuery or javascript?
Tired this way,but not working
$(link['style/style.css']).remove();
I got a line on the top of the page,
<link rel="stylesheet" href="style/style.css">
How do I remove it by jQuery or javascript?
Tired this way,but not working
$(link['style/style.css']).remove();
jQuery selection by attribute is in this format:
$('link[href="style/style.css"]');
Then you need to disable it:
// prop() suggestion from @jahroy
$('link[href="style/style.css"]').prop("disabled", true);
In most modern browsers you can iterate through the list of stylesheets and disable the ones you want:
var toRemove = 'style/style.css';
[].forEach.call(document.styleSheets, function(styleSheet) {
if (styleSheet.href && styleSheet.href.substr(-toRemove.length) === toRemove) {
styleSheet.disabled = true;
}
});
This is not a direct answer to your question but it may be a better way to achieve what you are trying to do:
If you are controlling the stylesheet and you want an easy way to turn it on and off then you may consider using the same trick that is used by Modernizr. Instead of having in your stylesheet:
.some-class { color: red }
and then removing the entire stylesheet it in some cases, you can have:
.style-x .some-class { color: red }
and then in your HTML or JavaScript you can add "style-x" class to the body tag:
<body class="style-x">
[...]
<div class="some-class">red text</div>
[...]
</body>
Removing the style in JavaScript is just removing the class style-x from the body tag, using .className
in plain JavaScript or .removeClass()
in jQuery.
That way you can have many "styles" in one concatenated and minified CSS file that can be activated and deactivated independently.
This javascript snippet works
how to remove css rel by javascript or jQuery
document.styleSheets[0].disabled = true;