2014-11-22 277 views
1

我想設置一個相對於另一個div的僞元素,但正如我注意到的,它不可能直接設置它,所以我試圖將它添加到頭中的元素,但你可以如果你這樣做的話,不要使用變量,我需要它。通過jQuery設置僞元素高度

這是我的代碼。

var sectionHeight = 0; 
     $(window).load(function(){ 
      sectionHeight = $('.checkheight').outerHeight(true); 
      $("#section1::before").css({"height":sectionHeight}); 


     }) 

這就是我第一次,之前我知道我不能過jQuery的設置僞元素中的CSS屬性

,這是我現在有

var sectionHeight = 0; 
     $(window).load(function(){ 
      sectionHeight = $('.checkheight').outerHeight(true); 


     }) 
     $('<style>#section1:before{height: sectionHeight }</style>').appendTo('head'); 

,但它贏得不使用變量來設置高度。

有人會知道我的問題的適當解決方案嗎?

回答

2

你只需要在風格標籤追加onload處理

$(window).load(function(){ 
    var sectionHeight = $('.checkheight').outerHeight(true); 

    $('<style>#section1:before {height: ' + sectionHeight + 'px;}</style>').appendTo('head'); 
}); 
+0

它確實在元素中,但它會設置高度「sectionHeight」,你可以在這個截圖中看到:HTTP: //imgur.com/GofDyGO – Kairowa 2014-11-22 08:52:04

+0

@ user3808051 - 哦,沒有注意到,編輯了答案 – adeneo 2014-11-22 09:00:13