【问题标题】:Can't Get Styles in Non-Webkit Browsers with jQuery无法使用 jQuery 在非 Webkit 浏览器中获取样式
【发布时间】:2013-12-24 01:00:42
【问题描述】:

我正在使用this script 提取页面上元素的样式信息,然后将这些样式应用于第二个元素,但由于某种原因,它仅适用于 Chrome 和 Safari [不适用于 Firefox 或 Internet Explorer ]。这是有问题的,因为我主要需要 Internet Explorer。

这是演示:http://jsfiddle.net/J3tSx/1/

脚本:

$(document).ready(function() {
    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;
    }
    /*
    $("#test_div").click(function() {
        alert("clicked");
        if (!$(this).hasClass("hovered")) {
            alert("detected no hovered class");
            $(this).addClass("hovered");
            alert("added hovered class");
            var hoverStyle = css($("#test_div"));
            alert("created the hoverStyle variable");
            $("#second_div").css(hoverStyle);
            alert("applied the hoverStyle variable to #second_div");
        }
    });
    */
    var hoverStyle = css($("#test_div"));
    alert("created the hoverStyle variable");
    $("#second_div").css(hoverStyle);
    alert("applied the hoverStyle variable to #second_div");
});

HTML:

<section id="test_div">
</section>
<section id="second_div">
</section>

更新:奇怪的是 IE 和 Firefox 都没有抛出任何类型的错误。他们只是表现得好像#test_div 没有样式,因此没有样式被添加到#second_div。很奇怪。

更新 2:我刚刚注意到这段代码:

var s = {};
if (!css) return s;

我认为这可能与它有关。

我尝试过的事情

  • 更改部分的名称,以摆脱 _
  • 改用旧版本的 jQuery
  • 将代码的位置从身体移动到头部等

【问题讨论】:

    标签: jquery css internet-explorer webkit


    【解决方案1】:

    看起来该函数没有获取 IE 和 FireFox 中元素的所有样式。您可以使用console.log(hoverStyle) 查看正在检索的样式。似乎只有 colorheightwidth 被拉入 FireFox。

    试试这个作为第二个答案here 提供的解决方案。 (请务必为原帖点赞!)

    /*
     * getStyleObject Plugin for jQuery JavaScript Library
     * From: http://upshots.org/?p=112
     */
    
    (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);
    

    这适用于 FireFox 和 IE:FIDDLE

    【讨论】:

    • 这太完美了,谢谢!我肯定也会赞成原始答案。还有一件事:我希望将这些样式应用于一个类,然后我可以使用 jQuery 进行切换。这可能吗,还是我只需要清除内联样式,然后每次都重新生成它们?
    • 您可以使用 JavaScript 动态创建 CSS 类,但不建议这样做:(stackoverflow.com/questions/1720320/…)。 IMO 这似乎是内联样式的一个很好的用例。祝你好运=D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多