是否由样式表或内联样式设置

是否由样式表或内联样式设置

本文介绍了是否可以检测元素的样式是否只是浏览器的默认值?是否由样式表或内联样式设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JavaScript是包含在X的网站,但我没有任何其他控制她的网站或她包括它的地方。如果她的样式是 #element ,我想离开它,但如果她没有,我有一个样式表我会注入 < head> 。是否有任何方法可以检测是否有风格?

My javascript is included in X's site, but I don't have any other control over her site or where she includes it. If she's styled #element, I want to leave it alone, but if she hasn't, I've got a stylesheet I'll inject in <head>. Is there any way to detect whether or not she's styled it?

检查它的高度是0或1失败,因为它有一些内容,浏览器做出默认决定。

Checking it's height is 0 or 1 fails because it's got some content in it and the browser makes default decisions.

对于javascript / jquery /其他框架来说,任何方式告诉CSS风格的CSS特性都会回答这个问题,并且令人难以置信。

推荐答案

对于内联样式,很容易:你可以访问 element.style 对象并检查您想要的属性 - 或使用 element.hasAttribute('style')来检查属性是否在HTML中定义。

For inline style, it's quite easy: you can just access to element.style object and check the property you want to – or use element.hasAttribute('style') to check if the attribute is defined in the HTML.

对于通过样式表应用的CSS规则,我建议您查看

For CSS rules applied by stylesheet, I would suggest to you to take a look at "Is it possible to check if certain CSS properties are defined inside the style tag with Javascript?" that it's quite similar from what are you asking.

使用这里描述的方法,也很容易过滤只有#的规则。元素选择器,如果你想过滤掉任何通用规则(如标签)。

Using the approach described there, is also quite easy filtered the rules that have only the #element selector in, if you want to filter out any generic rule (like tag ones).

这篇关于是否可以检测元素的样式是否只是浏览器的默认值?是否由样式表或内联样式设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 18:43