javascript - 保护元素不受全局 CSS 影响

标签 javascript html css

我的 js 嵌入了第三方网站,它创建了一个 <iframe>其中包含一个简单的评论面板,但显然在这个特定的网站上有一个 CSS 样式表,它为每个 <iframe> 设置样式。使用 !important 在 dom 中标记flag ,所以我不能改变那些 css 规则,网站开发团队也不会改变这种行为,还有另一种方法可以克服这个问题吗?我可以更改 tagname 吗仍然是一个 iframe?任何事物?

最佳答案

您可以使用 all 属性和 initial 值来为该元素设置默认样式。

来自docs :

The all CSS shorthand property sets all of an element's properties (other than unicode-bidi and direction) to their initial or inherited values, or to the values specified in another stylesheet origin.

代码示例:

#div-to-reset-styles {
  all: initial;
  * {
    all: unset;
  }
}

只需针对您的特定 iframe 就可以了。

关于javascript - 保护元素不受全局 CSS 影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53497074/

相关文章:

html - 如何在 Chrome 扩展中使用 .ttf 字体?

jQuery 向左 move

html - 当我运行 Grunt 时,它正在删除我的 CSS 文件

javascript - 如何在未完成添加序列的情况下从 rxjs 创建数组

javascript - 重绘 Canvas html5而不闪烁

javascript - 如何编辑由 ruby​​gem 创建/引用的 .js 文件?

javascript - 如何禁用向左滚动?

css - 边缘输入占位符与文本不对齐

javascript - 将 ajax 值传递到 Laravel Controller

javascript - 从 Angular 5 中的子组件更新父 bool 值