2014-11-22 276 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