2013-11-21 87 views
-1

我正在創建一個jQuery插件,我需要使用另一個元素的精確填充來創建元素。將這個已經足夠了:元素的複製填充樣式

$(this).css('padding', otherElement.css('padding')); 

,或者我應該檢查所有:

$(this).css({ 
    'padding', otherElement.css('padding'), 
    'padding-left', otherElement.css('padding-left'), 
    'padding-right', otherElement.css('padding-right'), 
    'padding-top', otherElement.css('padding-top'), 
    'padding-bottom', otherElement.css('padding-bottom'), 
}); 
+1

你嘗試了嗎? – Novocaine

+4

這實際上是一個JSFiddle中的2分鐘工作 - http://jsfiddle.net/AVZDC/ – ahren

+1

爲什麼不自己嘗試? – gvee

回答

1

是的,它會奏效。

嘗試:

$("#div2").click(function() { 
    $(this).css("padding", $("#div1").css("padding")); 
}); 

Fiddle here.

+0

以及我的意思是,如果沒有定義填充,只有填充左定義,將'$(this).css(「padding」,$(「#div1」).css(「padding」));'工作? –

+0

@Ashkan Mobayen Khiabani。答案是肯定的... http://jsfiddle.net/AVZDC/1/ – SW4

+0

@NathanLee謝謝:) – Hiral