gpt4 book ai didi

javascript - 重置小部件的所有 CSS 样式

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:09 25 4
gpt4 key购买 nike

我正在创建一个使用许多自定义样式的 Javascript 小部件。当用户将小部件放在他的页面上时,用户的页面总是会影响小部件的样式。有办法避免这种情况吗?

例如,如果我的小部件中有一个输入框:

<input id="#special-input" />

并且用户具有覆盖输入的样式:

input { width: 500px; }

然后,用户的页面将覆盖输入框的宽度。用户可以通过多种方式更改小部件的样式,我无法解释所有这些方式。例如,对于输入,我必须考虑边框、阴影、宽度、高度等,以及用户可以设置页面样式并影响我的小部件的任何其他方式。

最佳答案

为了回馈我上面的评论,您可以执行以下操作。 (完全未经测试,很快拼凑起来)这使用了 Jquery,但我相信如果您使用另一个库或纯 JS,您可以弄清楚如何修改它。

var allStyles = ["azimuth","background","backgroundAttachment","backgroundColor","backgroundImage","backgroundPosition","backgroundRepeat","border","borderBottom","borderBottomColor","borderBottomStyle","borderBottomWidth","borderCollapse","borderColor","borderLeft","borderLeftColor","borderLeftStyle","borderLeftWidth","borderRight","borderRightColor","borderRightStyle","borderRightWidth","borderSpacing","borderStyle","borderTop","borderTopColor","borderTopStyle","borderTopWidth","borderWidth","bottom","captionSide","clear","clip","color","content","counterIncrement","counterReset","cssFloat","cue","cueAfter","cueBefore","cursor","direction","display","elevation","emptyCells","font","fontFamily","fontSize","fontSizeAdjust","fontStretch","fontStyle","fontVariant","fontWeight","height","left","letterSpacing","lineHeight","listStyle","listStyleImage","listStylePosition","listStyleType","margin","marginBottom","marginLeft","marginRight","marginTop","markerOffset","marks","maxHeight","maxWidth","minHeight","minWidth","orphans","outline","outlineColor","outlineStyle","outlineWidth","overflow","padding","paddingBottom","paddingLeft","paddingRight","paddingTop","page","pageBreakAfter","pageBreakBefore","pageBreakInside","pause","pauseAfter","pauseBefore","pitch","pitchRange","playDuring","position","quotes","richness","right","size","speak","speakHeader","speakNumeral","speakPunctuation","speechRate","stress","tableLayout","textAlign","textDecoration","textIndent","textShadow","textTransform","top","unicodeBidi","verticalAlign","visibility","voiceFamily","volume","whiteSpace","widows","width","wordSpacing","zIndex"];

$.each(allStyles, function(key, value){
if ($this.css(value) !== undefined){
$this.css(key, "")
}
});

注意 我上面的帖子是根据这个关于另一个主题的答案修改的。只是想感谢@Matrym ... getComputedStyle (or) $.css(map) <-- to get every style declaration

关于javascript - 重置小部件的所有 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18240759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com