简体   繁体   English

如何删除所有链接的样式表,但按ID保留一个?

[英]How to remove all linked stylesheets but keep one by ID?

I need to remove all linked stylesheets in the head , while keeping one of them. 我需要删除头部中所有链接的样式表,同时保留其中之一。

I want them all removed , except for the one with id=custom_stylesheet 我希望将其全部删除,但id = custom_stylesheet的除外

<link type="text/css" href="" rel="stylesheet"></link>
<link type="text/css" href="" rel="stylesheet"></link>
<link type="text/css" href="" rel="stylesheet"></link>
<link id="skin_stylesheet" type="text/css" href="" rel="stylesheet"></link>
<link id="custom_stylesheet" type="text/css" href="" rel="stylesheet"></link>

This worked to remove them all , but how do i edit it to keep one ? 这可以删除所有内容,但是我如何对其进行编辑以保留其中一个?

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

You can use :not or .not() : 您可以使用:not.not()

$('link[rel=stylesheet]:not(#custom_stylesheet)').remove();
$('link[rel=stylesheet]').not('#custom_stylesheet').remove();

There is a document.styleSheets collection that contains all the style sheet for a document. 有一个document.styleSheets集合 ,其中包含文档的所有样式表。 The advantage is that it includes all style sheets, ie those added by link elements and style elements. 优点是它包括所有样式表,即由链接元素和样式元素添加的样式表。

So iterate over that: 因此遍历:

var sheet, sheets = document.styleSheets;

for (var i=0, iLen=sheets.length; i<iLen; i++) {
  sheet = sheets[i];

  if (sheet.id != 'custom_stylesheet') {
    sheet.parentNode.removeChild(sheet);
  }
}

I'm sure there's a jQuery–ised version that uses each , something like: 我敢肯定有一个使用的每个 ,像一个jQuery的ISED版本:

jQuery.each(document.styleSheets, function(i, sheet) {
  if (sheet.id != 'custom_sytleSheet') $(sheet).remove();

  // or much more efficient
  // if (sheet.id != 'custom_sytleSheet') sheet.parentNode.removeChild(sheet)
})

try 尝试

 $('link[rel=stylesheet]').filter(function(){
    return  this.id !="custom_stylesheet";
 }).remove();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将链接的样式表动态应用于文档-是全部添加还是动态添加/删除? - Applying linked stylesheets to document dynamically - add all, or add/remove dynamically? 如何从一个js文件加载所有脚本,样式表和标记? - How to load all scripts, stylesheets, and markup from one js file? "JS - jQuery 删除所有包含一个类的带区,但保留带有 URL 中 ID 的带区" - JS - jQuery Remove all bands that contains a class but keep the one with the ID that is in the URL 如何计算 ID 相同的平均值,然后删除该 ID 的重复项,但保留 javascript 中的平均值 - How to calculate average where ID is same and then remove duplicates of that ID but keep the one which has average in javascript 如何从具有相同ID的数组中删除多个项目,但在增加其“数量”的同时保留一个项目? - How to remove multiple items from an array with the same id, but keep one while increasing its “qty”? JavaScript-无论如何要删除对象的所有基本方法和属性,但要保留其中一个? - JavaScript - anyway to remove all base methods and attributes of an Object but keep one? 保留但停用样式表和脚本 - Keep, but deactivate stylesheets & scripts 如何解析文档并删除除匹配和ID及其子项之外的所有元素 - How can I parse a document and remove all elements except for one that matches and ID and its children 如何删除所有具有空ID的元素 - How to remove all the elements with null id 如何删除获取ID的一部分 - how to remove one part of the getting id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM