DEMO 这个函数在firefox中不起作用,但100%在chrome中工作。这是什么原因?
我创建了一个js代码来保存元素css以供以后使用。它正在使用chrome而不是在firefox中为什么会这样?
function css(a){
var sheets = document.styleSheets, o = {};
for(var i in sheets) {
var rules = sheets[i].rules || sheets[i].cssRules;
for(var r in rules) {
if(a.is(rules[r].selectorText)) {
o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
}
}
}
return o;
}
function css2json(css){
var s = {};
if(!css) return s;
if(css instanceof CSSStyleDeclaration) {
for(var i in css) {
if((css[i]).toLowerCase) {
s[(css[i]).toLowerCase()] = (css[css[i]]);
}
}
} else if(typeof css == "string") {
css = css.split("; ");
for (var i in css) {
var l = css[i].split(": ");
s[l[0].toLowerCase()] = (l[1]);
};
}
return s;
}
var style = css($("#elementToGetAllCSS"));
$("#elementToPutStyleInto").css(style);
var style = css($("#elementToGetAllCSS1"));
$("#elementToPutStyleInto1").css(style);
答案 0 :(得分:0)
Firefox中存在冲突,现在已修复错误
(function($){
$.fn.getStyleObject = function(){
var dom = this.get(0);
var style;
var returns = {};
if(window.getComputedStyle){
var camelize = function(a,b){
return b.toUpperCase();
};
style = window.getComputedStyle(dom, null);
for(var i = 0, l = style.length; i < l; i++){
var prop = style[i];
var camel = prop.replace(/\-([a-z])/g, camelize);
var val = style.getPropertyValue(prop);
returns[camel] = val;
};
return returns;
};
if(style = dom.currentStyle){
for(var prop in style){
returns[prop] = style[prop];
};
return returns;
};
return this.css();
}
})(jQuery)
var styles = $("#elementToGetAllCSS").getStyleObject();
$("#elementToPutStyleInto").css(styles);
var styles1 = $("#elementToGetAllCSS1").getStyleObject();
$("#elementToPutStyleInto1").css(styles1);