【问题标题】:Function Returns Different Values in Different Browsers函数在不同的浏览器中返回不同的值
【发布时间】:2023-04-07 19:30:01
【问题描述】:

我创建了一个函数,它返回数组中元素的选定 css 属性。然而,这个数组在 Chrome 和 Firefox 中有所不同。 小提琴:http://jsfiddle.net/9Jyx8/1/

这是函数:

function butunCSSyiAl (el,ozellikler) {
    var deger = new Array();
    for (var i = 0 ; i < ozellikler.length ; i++) {
        deger[i] = el.css(ozellikler[i]);
        if(ozellikler[i] == "width"){
        //alert(el.css(ozellikler[i]));
        }
    }
    return deger;
}



 var ozellikler = new Array('font-size','font-weight','font-style','color','text-transform','text-decoration','letter-spacing','word-spacing',  'line-height','text-align','vertical-align','direction','background-color','background-image','background-repeat','background-position','background-attachment','width','height','top','right','bottom','left','margin-top','margin-right','margin-bottom','margin-left','padding-top','padding-right','padding-bottom','padding-left','border-top-width','border-right-width','border-bottom-width','border-left-width','border-top-color','border-right-color','border-bottom-color','border-left-color','border-top-style','border-right-style','border-bottom-style','border-left-style','position','display','visibility','overflow-x','overflow-y','white-space','clip','float','clear','cursor','list-style-image');

有没有办法解决这个问题?

【问题讨论】:

    标签: javascript jquery css cross-browser


    【解决方案1】:

    浏览器设置自己的默认样式,因此您的数组在不同浏览器之间会有不同的值。每个浏览器可能对font-size、边距等有不同的默认值。

    【讨论】:

    • 所以我应该避免尝试获取默认值。谢谢。
    • 没错。正如@adeneo 在 cmets 中所说,您可以使用 CSS 重置(搜索 Google 或此站点以了解如何执行此操作;有很多示例)。否则,默认值将永远不会一致,因此您的数组也不会一致。如果这对您有帮助,请记住接受它作为答案! :)
    猜你喜欢
    • 2010-10-24
    • 2018-02-16
    • 2012-03-25
    • 2015-02-13
    • 2016-03-18
    • 2016-03-27
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多