2014-03-03 43 views
1

我需要将额外的“margin-top”应用于主体中的特定类。这个类的名字已经在一个CSS文件中记载:将样式应用于特定的主体类

html body.admin-menu { 
    margin-top:29px !important; 
} 

不过,我需要在我的jQuery的地方这边距更改为60像素。

我已经试过这4个选项没有雪茄:

$('html body.admin-menu').addClass('marginfix'); 

$('html body.admin-menu').attr('style', 'margin-top:60px !important'); 

$('html body.admin-menu').attr('style', function(i,s) { return s + 'margin-top: 60px !important;' }); 

$('<style>.marginfix { margin-top:60px !important; }</style>').appendTo('html body.admin-menu'); 

我似乎可以影响的唯一因素是“HTML”但我需要这种风格适用于这个非常特殊的情况下(HTML body.admin -menu),而不仅仅是“html”标签。

任何人都知道什么会工作?

回答

1

不知道但在我看来,你是错过了文档就绪的方法。看起来你已经准备好了,将这个类添加到元素中。

这应该工作:

$(function(){ 
    $('html body.admin-menu').addClass('marginfix'); 
}); 

或试试这个:

$(function(){ 
    $('html body.admin-menu').css({marginTop: '60px'}); 
}); // above code will make a inline css to the body. 

尝试敷在文档准备好方法,我想你在你的CSS文件中有这个CSS类.marginfix风格某处。尽管您可以从选择器中省略html

这个CSS应该提供这样的:

html body.admin-menu { 
    margin-top:29px !important; 
} 

.marginfix{ 
    margin-top:60px !important; // this overrides above class applied to body. 
} 
+0

感谢您的回复,但是,这似乎并没有工作。 – WebMW

+0

你在浏览器控制台中是否有任何错误? – Jai

+0

我很抱歉,它实际上是添加了类,(在控制台中没有错误),但是margin-top不适用于body。填充顶部的作品,但任何想法,为什么这可能是? – WebMW

0

试试这个:

$('body.admin-menu').css({'margin-top':'60px'}); 

,并从你的CSS删除!important。只需在所有其他CSS之后添加body.admin-menu部分,以覆盖任何以前的样式。

+0

因为.admin-menu是只有在登录管理员时才识别的类。 – WebMW

+0

好的,编辑我的答案.. – Hardy

+0

感谢编辑,但是,这似乎并没有工作。 – WebMW

0

你可以做这样的事情的样式添加到您的<head>

$('<style type="text/css">.marginfix { margin-top:60px !important; }</style>').appendTo($('head')); 

然后你打电话给你addClass()方法:

$('body.admin-menu').addClass('marginfix'); 
+0

感谢您的回复,但是,这似乎并没有工作。 – WebMW

相关问题