Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将样式应用于特定的主体类_Javascript_Jquery - Fatal编程技术网

Javascript 将样式应用于特定的主体类

Javascript 将样式应用于特定的主体类,javascript,jquery,Javascript,Jquery,我需要对正文中的特定类应用额外的“边距顶部”。该类的名称已存在于CSS文件中,并读取: html body.admin-menu { margin-top:29px !important; } 但是,我需要在jQuery中找到一个位置,将这个边距上限更改为60px 我在没有雪茄的情况下尝试了以下4种选择: $('html body.admin-menu').addClass('marginfix'); $('html body.admin-menu').attr('style', 'm

我需要对正文中的特定类应用额外的“边距顶部”。该类的名称已存在于CSS文件中,并读取:

html body.admin-menu {
   margin-top:29px !important;
}
但是,我需要在jQuery中找到一个位置,将这个边距上限更改为60px

我在没有雪茄的情况下尝试了以下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 body.admin menu').addClass('marginfix');
$('html body.admin menu').attr('style','margintop:60px!important');
$('html body.admin menu').attr('style',function(i,s){returns s+'margin top:60px!important;'});
$('.marginfix{margin top:60px!important;}').appendTo('html body.admin menu');
我似乎能影响的唯一元素是“html”,但我需要将此样式应用于这个非常具体的案例(html body.admin菜单),而不仅仅是“html”标记

有人知道什么有用吗?

试试这个:

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

并删除该
!重要信息
来自您的CSS。只需在所有其他CSS之后添加
body.admin菜单
部分,这样它就可以覆盖任何以前的样式。

不确定,但在我看来,您缺少doc ready方法。似乎您正在将类添加到元素中,但尚未准备就绪

这应该起作用:

$(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.
尝试用doc-ready方法包装它,我想您在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.
}

您可以这样做,将样式添加到您的


因为.admin菜单是一个仅在有登录管理员时才识别的类。感谢您的编辑,但是,这似乎不起作用。感谢您的回复,但是,这似乎不起作用。我很抱歉,它实际上是在添加类,(控制台中没有错误),但页边空白顶部不适用于正文。填充顶部是可行的,但知道为什么会这样吗?我希望这个类名有
!重要信息
css hack,它应该在css类
body.admin菜单下的css文件中提供。css中的“margintop”似乎对body没有任何作用。然而,我使用了相对定位&top:60px;使用自定义样式(在您的第一个选项中)使其工作。非常感谢。谢谢你的回复,但是,这似乎不起作用。
$('<style type="text/css">.marginfix { margin-top:60px !important; }</style>').appendTo($('head'));
$('body.admin-menu').addClass('marginfix');