如何在伪类之前和之后应用CSS? [英] How to apply the css for before and after pseudo classes?

查看:58
本文介绍了如何在伪类之前和之后应用CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

演示

我有以下html ...

I have the following html...

<div id="q">
  <dl>
    <dt><p>lorem ipsum</p></dt>
    <dd><span>lorem ipsum</span></dd>
    <dd><span>lorem ipsum</span></dd>
    <dd><span>lorem ipsum</span></dd>
    <dd><span>lorem ipsum</span></dd>
  </dl>
</div>

这是我的案例中极其需要的CSS ...

And this is extremely required css for my case...

#q dd:before{
    content: " ";
    /*height: 65px;*/
}

但是需要根据内容创建高度,就好像高度是动态的一样.因此,我需要使用jQuery创建它....

But the height is needed to be created as per the contents as if the height is dynamic. Thus, I need to create it with jQuery....

//for test 
var h = '65px';
//problem occurs here
$('#q dd:before').css('height',h);

检查时,高度未添加到#q dd:before选择器中.

When you inspect it, the height is not added in the #q dd:before selector.

似乎:before:after伪类.

您对jQuery或javascript有任何想法吗?

Do you have any idea with jQuery or javascript?

推荐答案

您不能使用jQuery定位伪元素,而是可以向dd元素中添加类:

You cannot target pseudo elements with jQuery, instead you can add a class to the dd elements:

$('#q dd').addClass('active');

然后根据您的CSS中的此类设置样式:

then set the style based on this class in your CSS:

#q dd.active:before{
    height: 65px;
}

更新了小提琴

Updated Fiddle

如果要使用动态值,则可以设置<style>,然后将其附加到<head>部分:

If you want to use dynamic value, then you can set a <style> then append it to the <head> section:

var h = '65px';

$('<style>#q dd:before{height:'+h+'}</style>').appendTo('head');

更新了小提琴

Updated Fiddle

这篇关于如何在伪类之前和之后应用CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆