你能检测到 dom 节点的样式何时设置为“auto”吗?
通过示例 CSS:
.thing { height: auto }
和 HTML:
<div class="thing">The quick brown fox jumps over a lazy dog.</div>
是否可以检测到 .thing 的高度设置为“auto”?
以下方法返回值:
jQuery('.thing').height() // n
jQuery('.thing').css('height') // 'npx'
getComputedStyle(node).height // 'npx'
是否有任何方法可以告诉我浏览器正在从“auto”计算这些值?
With the example CSS:
.thing { height: auto }
and HTML:
<div class="thing">The quick brown fox jumps over a lazy dog.</div>
is it possible to detect that the height of .thing is set to 'auto'?
The following methods return values:
jQuery('.thing').height() // n
jQuery('.thing').css('height') // 'npx'
getComputedStyle(node).height // 'npx'
Is there any method that will tell me that the browser is calculating these values from 'auto'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,有一种方法,但它并不好笑。您需要做的是:
styletags
和链接的样式表。然后获取所有样式标签中所有
cssRules
的selectorText
或者对于 IE < 9
获取所有元素的父元素
id
、class
和tagName
以找出标签获取属性的可能方式。cssRules
列表cssRules.style.width
处的cssRules
如果它是自动的。或者以相反的方式进行操作,找到所有带有
style.width == 'auto';
的cssRules
,无论哪种方式,如果没有大量代码,都不容易获得Yes there is a way, but it's not a funny one. What you have to do is:
styletags
and linked stylesheets.Then get the
selectorText
for allcssRules
in all style tagsor for IE < 9
Get all of your elements parents
id
,class
andtagName
to find out what possible ways for the tag to get the attribute.list of
cssRules
cssRules
atcssRules.style.width
if it is auto.or do it some reverse way and find all
cssRules
withstyle.width == 'auto';
either way its not something easy to get without a lot of code希望它有帮助,否则你有很多挖掘工作要做:)
对于你看到
[0]
的第二个选项意味着你必须循环,因为可能有不同的文件名等...<完整示例:
请注意
如果您已包含,则必须从跨域中转义这些内容。
它将不起作用,因为这可能被视为安全风险(跨域)。 ..
hope it helps, otherwise you have alot of digging to do :)
For the second option where you see
[0]
means you have to loop as there may be different file names, etc etc...full example :
PLEASE NOTE
You must escape those from cross domain.e.g. if you have included
<link ....="...jquery.com.../ui.css" />
it will not work as this might be considered as security risk (cross domain)...这不是最有效的解决方案,特别是对于旧的 IE 版本,但它应该工作得很好:
这是我的实现:
注意:
height: auto !important;
规则,在这种情况下,您总是必须走很长的路。和
This isn't the most efficient solution, particularly for old IE versions, but it should work pretty well:
<div style="clear: left; height: 30px">Test</div>
Here's my implementation:
Notes:
height: auto !important;
rule in the CSS, in which case you'd always have to go the long route.<img>
and<br>
这是上述建议的更完整实现:
您需要实现
asSpecific(a, b)
,如果 css 选择器a
至少与选择器b
,例如p#foo a#bar
比p.foo
更具体。您还需要考虑!important
标志。这可能有用: http://www.w3.org/TR/CSS2/ cascade.html#specificity
这应该向每个元素添加一个数据属性,指定它在 CSS 中是否具有自动高度样式,但您还需要检查样式属性并考虑默认样式。
Here's a more complete implementation of the above suggestions:
You'll need to implement
asSpecific(a, b)
which should work out if css selectora
is at least as specific as selectorb
, e.g.p#foo a#bar
is more specific thanp.foo
. You also need to take into account the!important
flag.This might be useful: http://www.w3.org/TR/CSS2/cascade.html#specificity
This should add a data property to each element specifying whether or not it has an auto height style in the CSS, but you'll also need to check the style attribute and think about default styles.