Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';使用jQuery无法在非Webkit浏览器中获取样式_Jquery_Css_Internet Explorer_Webkit - Fatal编程技术网

Can';使用jQuery无法在非Webkit浏览器中获取样式

Can';使用jQuery无法在非Webkit浏览器中获取样式,jquery,css,internet-explorer,webkit,Jquery,Css,Internet Explorer,Webkit,我用它来获取页面上某个元素的样式信息,然后将这些样式应用于第二个元素,但出于某种原因,它只在Chrome和Safari中工作[不适用于Firefox或Internet Explorer]。这是有问题的,因为我主要需要这个用于Internet Explorer 下面是演示: 脚本: $(document).ready(function() { function css(a) { var sheets = document.styleSheets, o = {};

我用它来获取页面上某个元素的样式信息,然后将这些样式应用于第二个元素,但出于某种原因,它只在Chrome和Safari中工作[不适用于Firefox或Internet Explorer]。这是有问题的,因为我主要需要这个用于Internet Explorer

下面是演示:

脚本:

$(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:

我想这可能与此有关

我尝试过的事情

  • 更改节的名称,以消除_
  • 更改为jQuery的旧版本
  • 将代码的位置从身体移动到头部等

看起来该函数没有获得IE和FireFox中元素的所有样式。您可以使用
console.log(hoverStyle)
查看正在检索的样式。FireFox中只显示了
color
height
width

尝试作为第二个答案提供的解决方案。(一定要投原帖的赞成票!)

/*
*jQuery JavaScript库的getStyleObject插件
*发件人:http://upshots.org/?p=112
*/
(函数($){
$.fn.getStyleObject=函数(){
var dom=this.get(0);
var风格;
var返回={};
if(window.getComputedStyle){
var camelize=函数(a,b){
返回b.toUpperCase();
};
style=window.getComputedStyle(dom,null);
for(变量i=0,l=style.length;i

这在FireFox和IE中可以使用。

这太完美了,谢谢!我肯定也会投票支持原来的答案。还有一件事:我希望将这些样式应用于一个类,然后可以使用jQuery进行切换。这是可能的,还是我需要清除内联样式,然后每次重新生成它们?您可以使用JavaScript动态创建CSS类,但不建议使用:()。在我看来,这似乎是内联样式的一个很好的用例。祝你好运
<section id="test_div">
</section>
<section id="second_div">
</section>
var s = {};
if (!css) return s;
/*
 * 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);