getCompatedStyle 对象包含方法吗?
我在这里设置了一个演示: http://jsbin.com/evifeb/
这更多的是我大声思考而不是一个正确的问题。 ?
为什么浏览器将样式规则与方法和保留字一起直接插入到计算样式对象中 它只会使解析变得困难。例如,您可能会注意到在我的演示中,我过滤掉了除字符串和数字之外的所有内容。这是为了清除同一范围内的函数。虽然,这不是 100% 准确,因为 length 属性值是一个数字。为什么不使用像“getAllStyles”这样的原型函数来返回样式对象而没有废话呢?
好的,所以我知道“getPropertyValue”,但这仅在您想要指定的样式规则时才有用。所以我想我想说的是:A)是否有返回这样一个交叉对象的正确方法浏览器安全吗? B)如果没有,除了 length 之外是否还有其他属性(不在 css 规范中)需要清除?
非常感谢您的帮助。我已经准备好拔牙了。
I have set up a demo here:
http://jsbin.com/evifeb/
This is more of me thinking out loud than a proper question but..
Why do browsers insert style rules directly into the computed styles object along side methods and reserved words? It just makes it difficult to parse.. For example, you may notice in my demo that I'm filtering out everything but strings and numbers.. this is to weed out the functions that are in the same scope. Although, that is not 100% accurate because the length property value is a number.. Why not have a prototype function like "getAllStyles" that returns an object of styles without the nonsense?
OKAY so I am aware of "getPropertyValue" but that is only useful if you want a specified style rule.. So I guess what I'm trying to say is: A) Is there a proper method of returning such an object that is cross browser safe? and B) If not, are there any other properties (not in the css spec) besides length that need weeding out?
Thanks so much for the help. I'm ready to pull out my teeth.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您的for
循环需要适量的Object.hasOwnProperty
。使用
hasOwnProperty()
过滤器总的来说,会解决问题,但只是治标不治本。 原因是您的代码使用for...in
循环来迭代数组。 不要这样做。使用
for...in
迭代对象,使用for
迭代数组。最后一件事:
getCompulatedStyle()
返回 的(只读)实例CSSStyleDeclaration
。使用提供的 API,一切都很简单:演示:http://jsbin.com/owenij/2
It sounds like yourfor
loops need a healthy dose ofObject.hasOwnProperty
.Using a
hasOwnProperty()
filter will, by and large, solve the problem, but it fixes the symptoms, not the cause. The cause is that your code uses afor...in
loop to iterate over an array. Don't do this.Use
for...in
to iterate over objects, and usefor
to iterate over arrays.One last thing:
getComputedStyle()
returns a (read-only) instance ofCSSStyleDeclaration
. Use the provided API and things are straightforward:Demo: http://jsbin.com/owenij/2