jQuery CSS插件返回计算的元素的样式来伪元素该元素? [英] jQuery CSS plugin that returns computed style of element to pseudo clone that element?

查看:124
本文介绍了jQuery CSS插件返回计算的元素的样式来伪元素该元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way using jQuery to return an object of computed styles for the 1st matched element. I could then pass this object to another call of jQuery's css method.

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

For example, with width, I can do the following to make the 2 divs have the same width:

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

如果我可以使文本输入看起来像现有的span

It would be nice if I could make a text input look like an existing span:

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

其中不带参数的.css()返回可传递给。css(obj)

where .css() with no argument returns an object that can be passed to .css(obj).

找不到一个jQuery插件,但它似乎应该存在,如果它不存在,我会把我的下面一个插件,并与所有的属性,我使用的帖子。)

(I can't find a jQuery plugin for this, but it seems like it should exist. If it doesn't exist, I'll turn mine below into a plugin and post it with all the properties that I use.)

基本上,我想要伪克隆某些元素,但使用不同的标签。例如,我有一个li元素,我想隐藏,并在它看起来相同的输入元素。当用户键入内容时,看起来像是在内联编辑元素

Basically, I want to pseudo clone certain elements but use a different tag. For example, I have an li element that I want to hide and put an input element over it that looks the same. When the user types, it looks like they are editing the element inline.

有任何建议吗?

这里是我目前有的。唯一的问题是获得所有可能的样式。这可能是一个可笑的长列表。

Here's what I currently have. The only problem is just getting all the possible styles. This could be a ridiculously long list.

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方法有一个例外:如果0参数传递,它返回计算的样式对象。

I've now been using the code above for awhile. It works well and behaves exactly like the original css method with one exception: if 0 args are passed, it returns the computed style object.

正如你所看到的,它会立即调用原来的css方法,如果是这样的话。否则,它获取所有列出的属性的计算样式(从Firebug的计算样式列表收集)。虽然它的值列表很长,但是它很快。希望对别人有用。

As you can see, it immediately calls the original css method if that's the case that applies. Otherwise, it gets the computed styles of all the listed properties (gathered from Firebug's computed style list). Although it's getting a long list of values, it's quite fast. Hope it's useful to others.

推荐答案

两年后,但我有你要找的解决方案。这是我写的一个插件(通过包装另一个人的插件格式的函数),这完全是你想要的,但在所有浏览器,甚至IE中所有可能的样式。

Two years late, but I have the solution you're looking for. Here's a plugin I wrote (by wrapping another guy's function in plugin format) which does exactly what you want, but gets all possible styles in all browsers, even 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

希望有帮助。

这篇关于jQuery CSS插件返回计算的元素的样式来伪元素该元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆