gpt4 book ai didi

javascript - 将样式应用于特定的主体类别

转载 作者:行者123 更新时间:2023-11-28 01:30:41 25 4
gpt4 key购买 nike

我需要对正文中的特定类应用额外的“margin-top”。该类的名称已存在于 CSS 文件中,内容如下:

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

但是,我需要在 jQuery 中留出一个位置来将这个 margin-top 更改为 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”,但我需要将此样式应用于这个非常具体的情况(html body.admin-menu),而不仅仅是“html”标签。

有人知道什么有效吗?

最佳答案

不确定,但在我看来,您缺少文档就绪方法。看起来您正在将类添加到元素准备就绪之前。

这应该有效:

$(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.
}

关于javascript - 将样式应用于特定的主体类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22146740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com