将非继承属性(例如文本颜色)级联到网页中的子元素,而不明确告诉子元素从父元素继承

发布于 2024-12-11 12:23:07 字数 607 浏览 0 评论 0 原文

有没有一种方法可以将文本颜色等非继承属性级联到网页中的子元素,而不明确告诉子元素从父节点继承

我正在尝试编写一个重新设计网页样式的小应用程序(例如google,msn.co.uk) )。通过执行以下操作将样式注入到正文中,样式包括颜色、背景色,

document.body.style.color='green';

但是正文中包含自己样式的元素不会继承颜色,例如具有自己样式的 div 元素

仍然显示蓝色。

我目前的理解是这是一个继承的问题,并不是所有的属性都会自动从父容器继承,而对于要继承的样式,一个 来更改要继承的颜色

document.getElementById('testing').style.color='inherit';

必须通过执行类似的操作 它会从 body 继承颜色,但是在这种情况下,我必须遍历所有子元素,明确告诉它们从父元素继承(我实际上可以将这些子元素设置为与 body 相同的颜色),

我想这是一个继承问题,并且不是具体问题

is there a way to cascade non-inherited properties like text color to child elements in a webpage without explicitly telling child elements to inherit from parent node

I am trying to write a small application which restyles a webpage(e.g. google,msn.co.uk). Styles include color, background color by injecting style into the body by doing the following

document.body.style.color='green';

however elements within the body which contained their own style do not inherit the color e.g. a div element with its own style <div id="testing" style="color": blue"> still displays blue.

my current understand is that this is an issue of inheritance and not all properties are automatically inherited form the parent container, and for the style to be inherited, one has to change the color to inherit by doing something like

document.getElementById('testing').style.color='inherit';

in which case it would inherit color from body. however in this case i would have to go through all child elements explicitly telling them to inherit from parent (i could actually have set these child elements to same color as body)

i guess this is an inheritance issue and not a specificity issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2024-12-18 12:23:07

我不知道是否有一种优雅而美好的方式来做你想做的事。但是:只是为了好玩,我尝试将 CSS 写入通过 JS 添加的

var overrides = document.createElement("style");
overrides.type = "text/css";
document.head.appendChild(overrides);

overrides.innerText = "body, body * { background: green !important }";

在开发者控制台中尝试一下,Stack Overflow 看起来就像圣帕特里克节!

再次强调,尚未在 Chrome 和 Chrome 之外进行测试。 Safari,但它可能可以满足您的需求。

I don't know if there's an elegant and nice way to do what you want. But: Just for fun, I tried writing CSS to a <style> element I added via JS, so I could use CSS selectors in the declarations and use the !important rule. It seemed to work (in Chrome & Safari, anyway), but it's somewhat hacky (but it's always a little hacky to change a site's styles)

var overrides = document.createElement("style");
overrides.type = "text/css";
document.head.appendChild(overrides);

overrides.innerText = "body, body * { background: green !important }";

Try it in the developer console, and Stack Overflow looks like St Patrick's day!

Again, haven't tested it outside of Chrome & Safari, but it might work for your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文