element.style或window.getComputedStyle()不显示float [英] element.style or window.getComputedStyle() does not showing float

查看:139
本文介绍了element.style或window.getComputedStyle()不显示float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将所有元素样式属性复制到另一个元素。 (内联和继承)
这样做我使用window.getComputedStyle(),但是当我采取所有的风格值, float 值不存在。
我甚至试图使用element.style,它不存在。



但是当我使用jQuery.css('float')我得到



你有没有任何解决方案或聪明的方法来做这件事?

window.getComputedStyle()在IE中不起作用< 9(docs 此处)。此外,当通过 element.style 访问float值时,该属性名为 cssFloat ,因为'float' JS中的字词(docs 此处)。在IE& 9,该属性命名为 styleFloat



您的示例通过jQuery工作的原因是因为jQuery知道固有的浏览器差异,并读取计算的样式,并平滑它们,以便您可以通过一致的API访问它们。



element.style 映射到样式属性,它不考虑继承/外部样式。对于旧IE,您需要的属性是 element.currentStyle ,它将继承的样式考虑在内。



你需要一个不依赖于jQuery的解决方案,你需要检查 window.getComputedStyle()并使用它,如果存在,并回退到 element.currentStyle (使用属性 styleFloat )。



希望有帮助,干杯!


I am trying to copy all element style properties, to another element. (both inline and inheritance) to do so I am using window.getComputedStyle(), but when I am taking all the style values the float value does not exist. I even tried to use element.style and it not exist there.

but when I am using jQuery.css('float') I am getting the right value back, so it's exists for sure!

do you have any solution or smart way to do it?

解决方案

Two things to consider: window.getComputedStyle() does not work in IE < 9 (docs here). Also, when accessing the float value via element.style, the property is named cssFloat because 'float' is a reserved word in JS (docs here). In IE < 9, the property is named styleFloat.

The reason your example works via jQuery is because jQuery knows of the inherent browser differences when reading calculated styles, and smoothes over them so that you can access them via a consistent API.

element.style maps to the style attribute, which does not consider inherited/external styles. For old IE, the property you want is element.currentStyle, which takes the inherited styles into account.

If you need a solution that does not rely on jQuery, you'll need to check for window.getComputedStyle() and use it if present, and fall-back to element.currentStyle for old IE (using the property styleFloat).

Hope that helps, cheers!

这篇关于element.style或window.getComputedStyle()不显示float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆