2014-03-03 42 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

相關問題