【问题标题】:Remove a single style from <style> inside <head> jQuery从 <head> jQuery 中的 <style> 中删除单个样式
【发布时间】:2018-03-23 05:49:47
【问题描述】:

我想删除单个样式,例如:

img{width:calc(100% - 10px)}

来自我的&lt;style&gt;,它是由&lt;head&gt; 内的插件动态插入的。我尝试了很多方法,但没有任何帮助。

ThElement.find('style').remove();

通过使用上面的代码,我可以一起删除所有&lt;style&gt;,但我需要删除唯一的单一样式。那么有没有什么办法呢。

我不想添加新样式,我只想删除现有的

这是给你的小提琴:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    html {
      margin: 0px;
      height: auto;
    }
    
    body {
      height: auto;
      padding: 10px;
      background: transparent;
      color: #000000;
      position: relative;
      z-index: 2;
      -webkit-user-select: auto;
      margin: 0px;
      overflow: hidden;
      min-height: 20px;
    }
    
    body:after {
      content: "";
      display: block;
      clear: both;
    }
    
    body::-moz-selection {
      background: #b5d6fd;
      color: #000;
    }
    
    body::selection {
      background: #b5d6fd;
      color: #000;
    }
    
    body,
    textarea {
      min-height: 500px !important;
    }
    
    body,
    body:focus {
      outline: transparent solid 0px;
    }
    
    body {
      background: transparent;
      position: relative;
      z-index: 2;
      overflow-x: auto;
      user-select: auto;
    }
    
    body a {
      user-select: auto;
    }
    
    body table td,
    body table th {
      border: 1px solid rgb(221, 221, 221);
    }
    
    body table td:empty,
    body table th:empty {
      height: 20px;
    }
    
    body table td,
    body table th {
      border: 1px double red;
    }
    
    body table td,
    body table th {
      border-width: 2px;
    }
    
    body table th {
      background: rgb(230, 230, 230);
    }
    
    body hr {
      clear: both;
      user-select: none;
      break-after: page;
    }
    
    body img {
      display: inline-block;
      float: none;
      vertical-align: bottom;
      margin-left: 5px;
      margin-right: 5px;
      max-width: calc(100% - 10px);
    }
    
    body {
      position: relative;
    }
    
    body ::after {
      position: relative;
      font-weight: normal;
    }
    
    body pre {
      white-space: pre-wrap;
      word-wrap: break-word;
    }
    
    body blockquote {
      border-left: 2px solid rgb(94, 53, 177);
      margin-left: 0px;
      padding-left: 5px;
      color: rgb(94, 53, 177);
    }
    
    body blockquote blockquote {
      border-color: rgb(0, 188, 212);
      color: rgb(0, 188, 212);
    }
    
    body blockquote blockquote blockquote {
      border-color: rgb(67, 160, 71);
      color: rgb(67, 160, 71);
    }
    
    body span {
      font-weight: normal;
      display: inline;
      line-height: 0;
    }
    
    body span {
      font-size: inherit;
      height: 1em;
      width: 1em;
      min-height: 20px;
      min-width: 20px;
      display: inline-block;
      margin: -0.2em 0.15em 0.2em;
      line-height: normal;
      vertical-align: middle;
    }
  </style>
</head>

<body>
 <img src="http://lorempixel.com/400/600/">
</body>

</html>

我只想从中删除body img 样式。

【问题讨论】:

  • 你能举一些你的风格的例子吗?我想查看 下的内容
  • 让我为你们制作一个小提琴。
  • 您可以选择元素并取消设置其样式,而不是通过编辑 DOM 来删除它。
  • 我为你们添加了一个小提琴。 @SandeshGupta 元素是动态的,可以创建更多或没有。所以我不知道你的解决方案到底是如何工作的。如果可以,我会很乐意使用它。
  • 您给出的示例有多种正文样式。那么,在这种情况下,您想如何删除?您将如何选择样式下的 CSS?按索引还是按名称?

标签: javascript jquery styles


【解决方案1】:

它看起来有点时髦,但您可以使用 String.replace()Regular Expression 删除特定规则。

类似这样的:

var style = $('head>style');
style.text( style.text().replace(/(img\s*\{[^}]*)max-width:\s*calc\(100%\s*-\s*10px\);([^}]*\})/g, "$1$2") );

JSFiddle example

或者,删除所有 img 样式更简单一些:

var style = $('head>style');
style.text( style.text().replace(/img\s*\{[^}]*\}/g, "img {}") );

Another JSFiddle

如果您需要删除更具体的选择器,则可以修改regex

var style = $('head>style');
style.text( style.text().replace(/body\simg\.fr-dii\s*\{[^}]*\}/g, "") );

One more JSFiddle

【讨论】:

  • 这对我有用,但你能帮我删除img 标签的所有样式吗?
  • 当然,我刚刚用另一个 JSFiddle 编辑了答案以显示这一点
  • 我的实际样式是body img.fr-dii,您的解决方案只为我删除了max-width,我不知道为什么。第二个也是同样的结果。!
  • 好的,也添加了这个来回答
【解决方案2】:

你可以覆盖头部样式

  1. 新标签
  2. img onload 回调 - 设置 css 宽度自定义大小

:)

【讨论】:

  • 欢迎来到StackOverflow,请仔细阅读问题并用您自己的想法和代码示例进行回答,为了获得更好的答案,您还应该解释您的回答。
猜你喜欢
  • 2014-03-23
  • 2014-12-05
  • 2014-06-17
  • 1970-01-01
  • 2018-10-22
  • 2013-06-28
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
相关资源
最近更新 更多