栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

返回元素的计算样式以伪克隆该元素的jQuery CSS插件吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

返回元素的计算样式以伪克隆该元素的jQuery CSS插件吗?

迟了两年,但我有您想要的解决方案。这是我写的一个插件(通过将另一个人的函数包装为插件格式),可以完全满足您的要求,但可以在所有浏览器(甚至是IE)中获得所有可能的样式。

jquery.getStyleObject.js:

(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;i<style.length;i++){     var prop = style[i];     var camel = prop.replace(/-([a-z])/g, camelize);     var val = style.getPropertyValue(prop);     returns[camel] = val; } return returns;        }        if(dom.currentStyle){ style = dom.currentStyle; for(var prop in style){     returns[prop] = style[prop]; } return returns;        }        return this.css();    }})(jQuery);

基本用法非常简单:

var style = $("#original").getStyleObject(); // copy all computed CSS properties$("#original").clone() // clone the object    .parent() // select it's parent    .appendTo() // append the cloned object to the parent, after the original     // (though this could really be anywhere and ought to be somewhere     // else to show that the styles aren't just inherited again    .css(style); // apply cloned styles

希望能有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号