var str = "bar";document.styleSheets[0].addRule('p.special:before','content: "'+str+'";');
var str = "bar";document.styleSheets[0].addRule('p.special:before', 'content: "' + str + '";');
p:before {content: "foo";color: red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="special">This is a paragraph</p><p>This is another paragraph</p>
var str = "bar";$('<style>p.special:before{content:"'+str+'"}</style>').appendTo('head');
var str = "bar";$('<style>p.special:before{content:"' + str + '"}</style>').appendTo('head');
p:before {content: "foo";color: red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="special">This is a paragraph</p><p>This is another paragraph</p>
如果我们谈论的是“操纵”值,而不仅仅是添加值,我们还可以使用不同的方法:
var str = window.getComputedStyle(document.querySelector('p'), ':before').getPropertyValue('content');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="special">This is a paragraph</p><p>This is another paragraph</p>
// Get the color value of .element:beforevar color = window.getComputedStyle(document.querySelector('.element'), ':before').getPropertyValue('color');
// Get the content value of .element:beforevar content = window.getComputedStyle(document.querySelector('.element'), ':before').getPropertyValue('content');
.box:before {content:var(--content,"I am a before element");color:var(--color, red);font-size:25px;}
<div class="box"></div><div class="box" style="--color:blue;--content:'I am a blue element'"></div><div class="box" style="--color:black"></div><div class="box" style="--color:#f0f;--content:'another element'"></div>
2)使用CSS和类
.box:before {content:var(--content,"I am a before element");color:var(--color, red);font-size:25px;}
.blue {--color:blue;--content:'I am a blue element';}.black {--color:black;}
document.querySelectorAll('.box')[0].style.setProperty("--color", "blue");document.querySelectorAll('.box')[1].style.setProperty("--content", "'I am another element'");
.box:before {content:var(--content,"I am a before element");color:var(--color, red);font-size:25px;}
<div class="box"></div><div class="box"></div>
4)使用jQuery
$('.box').eq(0).css("--color", "blue");/* the css() function with custom properties works only with a jQuery vesion >= 3.xwith older version we can use style attribute to set the value. Simply payattention if you already have inline style defined!*/$('.box').eq(1).attr("style","--color:#f0f");
.box:before {content:"I am a before element";color:var(--color, red);font-size:25px;}
The first argument to the function is the name of the custom property to be substituted. The second argument to the function, if provided, is a fallback value, which is used as the substitution value when the referenced custom property is invalid.
And later:
To substitute a var() in a property’s value:
If the custom property named by the first argument to the var() function is animation-tainted, and the var() function is being used in the animation property or one of its longhands, treat the custom property as having its initial value for the rest of this algorithm.
If the value of the custom property named by the first argument to the var() function is anything but the initial value, replace the var() function by the value of the corresponding custom property.
Otherwise, if the var() function has a fallback value as its second argument, replace the var() function by the fallback value. If there are any var() references in the fallback, substitute them as well.
Otherwise, the property containing the var() function is invalid at computed-value time.
If we don't set the custom property OR we set it to initial OR it contains an invalid value then the fallback value will be used. The use of initial can be helpful in case we want to reset a custom property to its default value.
$('body').css({backgroundColor: 'white'}).cssPseudo('after', {content: 'attr(title) ", you should try to hover the picture, then click it."',position: 'absolute',top: 20, left: 20}).cssPseudo('hover:after', {content: '"Now hover the picture, then click it!"'});
<style>.case-after:after { // set your properties here like eg:color:#3fd309 !important;}.case-before:before { // set your properties here like eg:color:#151715 !important;}</style>// case for after$('#button-id').on('click', function() {$(".target-div").toggleClass('case-after');});
// case for before$('#button-id').on('click', function() {$(".target-div").toggleClass('case-before');});
<h4> Type some text inside the box and click outside to see resule i.e. pseudo element content change</h4><divid="inputBoxParent"class="input-box-container"count="0"><inputtype="text"id="inputBox"placeholder="type some thing"onchange="handleOnChange(this.value)"onkeyup="handleOnChange(this.value)"/></div>