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

标签 javascript jquery css web-applications

我正在寻找一种使用 jQuery 返回第一个匹配元素的计算样式对象的方法。然后我可以将此对象传递给 jQuery 的 css 方法的另一个调用。

例如,width ,我可以执行以下操作使 2 个 div 具有相同的宽度:

$('#div2').width($('#div1').width());

如果我可以使文本输入看起来像现有的范围,那就太好了:

$('#input1').css($('#span1').css());

其中没有参数的 .css() 返回一个可以传递给 .css(obj) 的对象.

(我找不到用于此的 jQuery 插件,但它似乎应该存在。如果它不存在,我将在下面将我的变成一个插件并将其与我使用的所有属性一起发布。 )

基本上,我想伪克隆某些元素但使用不同的标签。例如,我有一个 li 元素,我想隐藏它并在它上面放置一个看起来相同的 input 元素。当用户输入时,看起来他们正在内联编辑元素

我也愿意接受其他方法来解决这个用于编辑的伪克隆问题。有什么建议吗?

这是我目前拥有的。唯一的问题是获取所有可能的样式。这可能是一个长得离谱的列表。


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;
}

编辑:我现在已经使用上面的代码一段时间了。它运行良好,行为与原始 css 方法完全一样,但有一个异常(exception):如果传​​递了 0 个参数,它会返回计算后的样式对象。

如您所见,如果适用,它会立即调用原始的 css 方法。否则,它会获取所有列出的属性的计算样式(从 Firebug 的计算样式列表中收集)。虽然它得到了一长串值,但速度相当快。希望它对其他人有用。

最佳答案

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

jquery.getStyleObject.js:

/*
 * getStyleObject Plugin for jQuery JavaScript Library
 * From: http://upshots.org/?p=112
 *
 * Copyright: Unknown, see source link
 * Plugin version by Dakota Schneider (http://hackthetruth.org)
 */

(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

希望对您有所帮助。

关于javascript - 返回元素的计算样式以伪克隆该元素的 jQuery CSS 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1004475/

相关文章:

javascript - 使用 Javascript 检测是否安装了字体,如果没有则提供不同的 css

jquery - 如何使用 freetile.js 居中元素?

javascript - jQuery 拖放 - 在 'Arts & Interests' 上对 Facebook 的 ui Tokenizer 进行逆向工程

css - 通过减少 css 文件来加速网站

javascript - Uniform.js 样式替代 SexyCombo 表单选择

javascript - 如何使用 java 脚本获取异常的子状态代码?像 500.1

javascript - AngularJS:基于链接输入字段的自定义验证

javascript - jquery 和 html 中的表分页

javascript - 在 Visualforce 页面中的字段下方添加错误消息

html - IE 7/8 渲染 Iinline (Floated Divs) 为 block : widths do not adjust to content?