【问题标题】:Set pseudo element height through jQuery通过jQuery设置伪元素高度
【发布时间】:2014-11-22 08:42:17
【问题描述】:

我正在尝试设置一个相对于另一个 div 的伪元素,但正如我所注意到的,无法直接设置它,所以我尝试将它添加到 head 的元素中,但如果你不能使用变量做,我需要。

这是我的代码。

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');

但它不会使用变量来设置高度。

有人知道我的问题的正确解决方案吗?

【问题讨论】:

    标签: jquery height pseudo-element


    【解决方案1】:

    您只需在 onload 处理程序中附加样式标签

    $(window).load(function(){
        var sectionHeight = $('.checkheight').outerHeight(true);
    
        $('<style>#section1:before {height: ' + sectionHeight + 'px;}</style>').appendTo('head');
    });
    

    【讨论】:

    • 它在 元素上做了,但它会将高度设置为“sectionHeight”,如您在此屏幕截图中所见:imgur.com/GofDyGO
    • @user3808051 - 哦,没注意到,编辑了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 2023-03-05
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多