有没有一种方法可以将文本颜色等非继承属性级联到网页中的子元素,而不明确告诉子元素从父节点继承
我正在尝试编写一个重新设计网页样式的小应用程序(例如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
发布评论
评论(1)
我不知道是否有一种优雅而美好的方式来做你想做的事。但是:只是为了好玩,我尝试将 CSS 写入通过 JS 添加的
!important
规则。它似乎可以工作(无论如何,在 Chrome 和 Safari 中),但它有点老套(但更改网站的样式总是有点老套)在开发者控制台中尝试一下,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)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.