在一些浏览器中(如火狐),getComputedStyle().getPropertyValue()不会为速记CSS报告任何内容,比如border。有没有一种非特定代码的方法来获得这些简写的CSS值?我已经考虑过建立一个速记CSS和它们各自的普通CSS值的白名单。但我意识到这样做既是一种巨大的痛苦,也是一种不向前兼容的设计。
发布于 2010-11-20 08:42:10
我想知道,你想对像border: 1px solid #000这样的字符串做什么?
假设您想要复制一个elems border以便复制它的copyStyle(el2, el, "border")
// Copies a set of styles from one element to another.
function copyStyle(dest, source, shorthand) {
var computed = window.getComputedStyle(source, null);
for (var i = computed.length; i--;) {
var property = camelize(computed[i]);
if (property.indexOf(shorthand) > -1) {
console.log(property)
dest.style[property] = computed[property];
}
}
}
// prototype.js
function camelize(text) {
return text.replace(/-+(.)?/g, function (match, chr) {
return chr ? chr.toUpperCase() : '';
});
}比较两个元素的给定样式集是否匹配可以以相同的方式完成。除此之外,我真的看不到使用字符串,如果你想用它计算任何东西,它应该被解析。
https://stackoverflow.com/questions/4230022
复制相似问题