javascript - insertRule ":before"除了一些

标签 javascript css

我可以使用 :

成功地“取消”所有包含“:before {content:}”的规则
document.styleSheets[0].insertRule('.main *:before {content: " "!important;}',0);

除了一些人之外,有没有一种方法可以做到这一点? 说走就走 “.main .submain:before”没有被触及?

我正在按照建议尝试:

document.styleSheets[0].insertRule('.main *:not(.submain):before {content: " "!important;}',0);

不幸的是,它不起作用。 然后我尝试使用以下方法在以前“无效”的规则中插入新的内容属性:

document.styleSheets[0].insertRule('.main .submain:before {content: "\fxxx"!important;}',0);

那也不行。

CSS如下:

.main {
  color: #bebedc;
  overflow: hidden;
 }
.main .submain:before{content:"\fzzz"}
.main .submain1:before{content:"\fxxx"}
.......................
.main .submain_n:before{content:"\fnnn"}

也许可以使用“querySelector('.submain')...”来做到这一点? 非常感谢您的建议。

目标是用自定义的替换“video js”播放器的原生 CSS“:before content”。因此,我示例中的“.main”类实际上是“.video-js”类。

在完整的 HTML 树下面:

<!doctype html>

<html>
<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, height=device-height, user-scalable=no, maximum-scale = 1.0, minimum-scale=1.0,initial-scale=1.0">

<title>Video JS </title>


<link href="https://unpkg.com/video.js/dist/video-js.min.css" rel="stylesheet">
<script src="https://unpkg.com/video.js/dist/video.min.js"></script>

</head>

<body>
<video id="myPlayer" class="video-js vjs-default-skin "  width="640" height="273" poster="https://vjs.zencdn.net/v/oceans.png"  controls preload="auto" 
data-setup='{}'>
<source src="https://vjs.zencdn.net/v/oceans.mp4" type="video/mp4"></source>

</video>

<script>

document.styleSheets[0].insertRule('.video-js *:before {content: " "!important;}',0);//---this "nulls"------all pseudo elements


//document.styleSheets[0].insertRule('.video-js * :not(.vjs-big-play-button):before {content: " "!important;}',0);//--attempt to leave ".vjs-big-play-button" untouched

//document.styleSheets[0].insertRule('.video-js *.vjs-big-play-button:before {content: "\f101"!important;}',0);//--attempt to re-insert ".vjs-big-play-button" after "nulling"


</script>
</body>
</html>

最佳答案

CSS 选择器可以使用 :not() 取反伪类。 (然而,最好让原始的 content 属性从一开始就只应用于所需的元素。)

div:before {
  content: "before ";
}

.main *:not(.submain):before {
  content: "" !important;
}
<div class="main">main
  <div class="submain">submain</div>
  <div class="foobar">foobar</div>
</div>

关于javascript - insertRule ":before"除了一些,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55485938/

相关文章:

javascript - 将 JSON 解析为 HTML 表

样式表中的 css 规则但未应用于浏览器

javascript - 移动菜单 (js) 未在 WordPress WPML 网站上加载

javascript - jQuery .load() 带有按名称输入选择器

javascript - 当用户将鼠标悬停在元素的框阴影上时,如何显示工具提示?

javascript - 向下滚动页面时标题向左移动(在 html 的 bootstrap 中使用词缀时)

javascript - 我无法转到 wwwroot ASP.NET 和 React.js 上的页面

php - 我一直收到这个 : Integrity constraint violation while trying to verify if email exists in database

css - Jest 中导入的样式对象为空

css - 是否可以在 block 元素的边距中注入(inject)背景属性?