gpt4 book ai didi

javascript - 使用 JQuery 获取所有样式属性。基本上我想要一个 getComputedStyle() 的等价物,我不想处理跨浏览器的东西

转载 作者:行者123 更新时间:2023-11-29 15:53:06 25 4
gpt4 key购买 nike

我想获取 currentStyle/computedStyle 对象,我不想手动检查浏览器的类型。 (我想摆脱跨浏览器处理,这就是我来到 Jquery 的原因)。实际上我想要样式对象返回所有样式属性:值映射。请帮忙。

最佳答案

好吧,Keith Bentrup 提供了一个类似问题的解决方案。 : jQuery CSS plugin that returns computed style of element to pseudo clone that element?

他使用了 Firebug 的属性列表,并使用 jQuery 创建了一个计算样式对象,以便能够将样式从一个对象克隆到另一个对象:

jQuery.fn.css2 = jQuery.fn.css;
jQuery.fn.css = function() {
if (arguments.length) return jQuery.fn.css2.apply(this, arguments);
var attr = ['font-family','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','opacity','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','z-index','overflow-x','overflow-y','white-space',
'clip','float','clear','cursor','list-style-image','list-style-position',
'list-style-type','marker-offset'];
var len = attr.length, obj = {};
for (var i = 0; i < len; i++)
obj[attr[i]] = jQuery.fn.css2.call(this, attr[i]);
return obj;
}

这似乎完全符合您的要求。

还有几个插件:

  1. http://github.com/peol/jquery-computed-style
  2. http://www.jupiterit.com/news/get-multiple-computed-styles-fast-with-the-curstyles-jquery-plugin

关于javascript - 使用 JQuery 获取所有样式属性。基本上我想要一个 getComputedStyle() 的等价物,我不想处理跨浏览器的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3044918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com