2011-01-06 63 views
4

如果有錯誤消息,我有以下代碼。那麼登錄表單的頁邊距應該爲「0」,對於第一個錯誤信息,頁邊距應該是「70px」。使用jQuery更改保證金值

<span class="portlet-msg-error"> You have entered invalid data. Please try again. </span> 

<span class="portlet-msg-error"> Authentication failed. Please try again.</span> 

<div class="login-form" style="margin-top: 70px;"> Form Display here </div> 

回答

8
$('.login-form').css({'margin-top':'0px'}); // Login form 
$('.portlet-msg-error:first').css({'margin-top':'70px'}); // Error Message 

這可能是marginTop,或者我忘了,如果jQuery的可以接受的全名,如果你有駝峯它時,有連字符

編輯剛纔檢查,margin-topseems to work

+0

兩個` 「的margin-top」:`和`marginTop:`工作。 jQuery將把camelCase版本轉換爲完整的CSS名稱。 – 2011-01-06 06:07:19

+0

那些是兩條相同的線?並且不正確的登錄框邊緣(基於問題)。 – 2011-01-06 06:12:18

+0

@ Box9:謝謝您的確認。 ;-) - @MarcusWhybrow:再也沒有。嘿,只是高招,現在是我上牀睡覺的時候了。 ;-) – 2011-01-06 06:13:17

1

您可以在CSS中執行此操作:

.login-form { 
    margin-top: 0px; 
} 

.portlet-msg-error:first-child { 
    margin-top: 70px; 
} 

jQuery中相同的是:

$('.login-form').css('margin-top', '0px'); 
$('.portlet-msg-error:first-child').css('margin-top', '70px'); 

所以你看有在這種情況下使用jQuery沒有真正的理由。

2

試試這個:

if(error) 
{ 
$(".login-form").css("margin-top", "0"); 
$(".portlet-msg-error:first").css("margin-top", "70px"); 
}