Javascript 使用 jQuery 更改保证金值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4612225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:16:29  来源:igfitidea点击:

changing margin value using jQuery

javascriptjqueryhtml

提问by Ritesh Patel

I have below code if there is error msg. then the margin top of login form should be "0" and for the first error msg margin top should be "70px".

如果有错误消息,我有以下代码。那么登录表单的边距顶部应该是“0”,对于第一个错误消息边距顶部应该是“70px”。

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

回答by Brad Christie

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

It may be marginTop, I forget if jQuery can accept the full name or if you have to camelCase it when there are hyphens

可能是marginTop,我忘记了 jQuery 是否可以接受全名,或者在有连字符时是否必须使用驼峰命名法

EDITJust checked, margin-topseems to work

编辑刚刚检查,margin-top似乎工作

回答by Chandu

Try this:

尝试这个:

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

回答by Marcus Whybrow

You can do this in CSS:

你可以在 CSS 中做到这一点:

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

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

The same in jQuery would be:

在 jQuery 中相同的是:

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

So you see there is no real reason to use jQuery in this case.

所以你看到在这种情况下没有真正的理由使用 jQuery。