如何从当前页面动态删除样式表 [英] How to dynamically remove a stylesheet from the current page

查看:155
本文介绍了如何从当前页面动态删除样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有从页面中动态删除当前样式表的方法?

Is there a way to dynamically remove the current stylesheet from the page?

例如,如果页面包含:

<link rel="stylesheet" type="text/css" href="http://..." />

...有没有办法以后用JavaScript禁用它?使用jQuery的额外点。

...is there a way to later disable it with JavaScript? Extra points for using jQuery.

推荐答案

好吧,假设你可以使用jQuery来定位它, c $ c> remove()上的元素:

Well, assuming you can target it with jQuery it should be just as simple as calling remove() on the element:

$('link[rel=stylesheet]').remove();

这将删除页面上的所有外部样式表。如果你知道部分URL,那么你可以只删除你正在寻找的:

That will remove all external stylesheets on the page. If you know part of the url then you can remove just the one you're looking for:

$('link[rel=stylesheet][href~="foo.com"]').remove();

且在Javascript中

And in Javascript

使用查询选择器和foreach数组全部删除的示例

this is an example of remove all with query selector and foreach array

Array.prototype.forEach.call(document.querySelectorAll('link[rel=stylesheet]'), function(element){
      try{
        element.parentNode.removeChild(element);
      }catch(err){}
    });

//or this is similar
var elements = document.querySelectorAll('link[rel=stylesheet]');
for(var i=0;i<elements.length;i++){
    elements[i].parentNode.removeChild(elements[i]);
}

这篇关于如何从当前页面动态删除样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆