將元素的計算樣式返回給偽克隆元素的jQueryCSS插件?我正在尋找一種使用jQuery返回第一個匹配元素的計算樣式對象的方法。然后,我可以將這個對象傳遞給jQuery的CSS方法的另一個調(diào)用。例如,用寬度,我可以做以下操作,使兩個div具有相同的寬度:$('#div2').width($('#div1').width());如果我能使文本輸入看起來像現(xiàn)有的跨度:$('#input1').css($('#span1').css());不帶參數(shù)的.css()返回可以傳遞給.css(Obj).(我找不到一個jQuery插件,但它似乎應該存在。如果它不存在,我將把下面的插件變成一個插件,并將它與我使用的所有屬性一起發(fā)布。)基本上,我想要偽克隆某些元素但是使用不同的標簽..例如,我有安莉元素,我想隱藏,并將一個輸入元素放在上面,看起來是一樣的。當用戶輸入時,看起來他們是在內(nèi)聯(lián)地編輯元素。.對于這個偽克隆問題,我也愿意使用其他方法進行編輯。有什么建議嗎?這是我目前所擁有的。唯一的問題是得到所有可能的樣式。這可能是一長得離譜的名單。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;}編輯:我已經(jīng)使用上述代碼一段時間了。它工作得很好,行為與原來的CSS方法完全一樣,只有一個例外:如果傳遞了0 args,它將返回計算樣式對象。如您所見,如果應用的是原來的CSS方法,它會立即調(diào)用該方法。否則,它將獲取所有列出的屬性的計算樣式(從Firebug的計算樣式列表中收集)。雖然它得到了一長串的值,但速度相當快。希望它對別人有用。
查看完整描述