简体   繁体   English

删除内联 CSS 会影响样式表中定义的 styles

[英]Removing inline CSS affects styles defined in stylesheet

I'm facing a strange behavior.我正面临一种奇怪的行为。

I have a script that applies inline CSS to elements from its stylesheet with computedStyle method.我有一个脚本,它使用computedStyle方法将内联 CSS应用于其样式表中的元素。

This is a way to copy rich text instead of plain text .这是一种复制富文本而不是纯文本的方法。

But after the copy, I need to delete the style="" attribute.但是复制后,我需要删除style=""属性。

When this happens, some style attributes (like color , disappears) while I can see in the inspector that this attribute is still linked to my element by its class .发生这种情况时,一些样式属性(如color消失),而我可以在检查器中看到该属性仍然通过其class链接到我的元素。

See there: https://jsfiddle.net/sxybrcug/ ( color disappears but border-color is okay)见那里: https://jsfiddle.net/sxybrcug/color消失但border-color没问题)

Do you know what's happening?你知道发生了什么吗?

Thanks谢谢

It looks like inline styles are being added to your container div as well, but on the button click, only the inline styles on the <p> elements are removed.看起来内联 styles 也被添加到您的container div 中,但是在单击按钮时,只有<p>元素上的内联 styles 被删除。 So p continues to inherit the inline styles of container , specifically, -webkit-text-fill-color , which is set to black, overriding the color property.所以p继续继承container的内联 styles ,具体来说, -webkit-text-fill-color设置为黑色,覆盖了color属性。

If you add如果你添加

container.setAttribute("style", "");

to your click listener, it removes all the inline styles and the color of the p elements shows as red.对于您的点击侦听器,它会删除所有内联 styles 并且p元素的颜色显示为红色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM