我需要获取:after
并将其分配给变量。这可能吗?
querySelectorAll
不起作用。
alert(some_div_with_pseudo.querySelectorAll('::after')[0]) // undefined
我需要获取:after
并将其分配给变量。这可能吗?
querySelectorAll
不起作用。
alert(some_div_with_pseudo.querySelectorAll('::after')[0]) // undefined
简短的回答是不行,还没有这个功能。
JavaScript可以访问DOM。在页面从HTML加载时构建DOM,在JavaScript修改它时进一步修改DOM。
伪元素是由CSS生成的,而不是HTML或JavaScript。它仅为了给CSS提供支撑而存在,但JavaScript完全不知道它的存在。
这就是正确的方式。在整体设计中,页面始于HTML。一方面,JavaScript可用于修改其行为和操作内容,而另一方面,CSS可用于控制结果的呈现:
HTML [→ JavaScript] → CSS → Result
你会发现CSS,包括伪元素,都在最后出现,因此JavaScript没有参与其中。
另请参见:
编辑
看来在现代 JavaScript 中,有一种使用 window.getComputedStyle(element,pseudoElement)
的解决方法:
var element = document.querySelector(' … ');
var styles = window.getComputedStyle(element,':after')
var content = styles['content'];
:hover
或:active
时获取更新的样式吗?似乎getComputedStyle
只返回伪元素的原始样式。然而,当普通元素被:hover
或:active
时,getComputedStyle
会返回更新后的样式。 - Levi UzodikequerySelector
中使用错误的选择器差不多,所以你对我的评论基本上是无关紧要的。所有这些都是字符串魔术值,直到发现错误才能发现它们... - undefined你可以这样做:
window.getComputedStyle(
document.querySelector('somedivId'), ':after'
);
:hover
或:active
时获取更新后的样式吗?似乎getComputedStyle
只返回伪元素的原始样式。但是,当普通元素被:hover
或:active
时,getComputedStyle
会返回更新后的样式。 - Levi Uzodikecontent
。#menu a[href*='sidebar']::after {content: '\2192' !important;}
JavaScript使用
css_rule_set('#menu a[href*="sidebar"]::after','content','"\u2192"','important');
JavaScript前提条件
var sidebar = 20;
function id_(id)
{
return (document.getElementById(id)) ? document.getElementById(id) : false;
}
function css_rule_set(selector,property,value,important)
{
try
{
for (var i = 0; i<document.styleSheets.length; i++)
{
var ss = document.styleSheets[i];
var r = ss.cssRules ? ss.cssRules : ss.rules;
for (var j = 0; j<r.length; j++)
{
if (r[j].selectorText && r[j].selectorText==selector)
{
if (typeof important=='undefined') {r[j].style.setProperty(property,value);}
else {r[j].style.setProperty(property,value,'important');}
break;
}
}
}
}
catch(e) {if (e.name !== 'SecurityError') {console.log('Developer: '+e);}}
}
function sidebar_toggle()
{
if (id_('menu_mobile')) {id_('menu_mobile').checked = false;}
if (getComputedStyle(id_('side')).getPropertyValue('display') == 'none')
{
css_rule_set('#menu a[href*="sidebar"]::after','content','"\u2192"','important');
if (is_mobile())
{
css_rule_set('main','display','none','important');
css_rule_set('#side','width','100%','important');
css_rule_set('#side','display','block','important');
}
else
{
css_rule_set('main','width',(100 - sidebar)+'%');
css_rule_set('#side','display','block');
}
}
else
{
css_rule_set('#menu a[href*="sidebar"]::after','content','"\u2190"','important');
if (is_mobile())
{
css_rule_set('main','display','block','important');
css_rule_set('main','width','100%','important');
css_rule_set('#side','display','none','important');
}
else
{
css_rule_set('main','width','100%','important');
css_rule_set('#side','display','none');
}
}
'#menu a[href*="sidebar"]::after'
的情况下),然后使用您提供的新值更新其中的单个声明?虽然这不是这个问题的用例,但是只有当已经存在针对伪元素的样式声明时,才能起作用,是吗? - Brice #box::after{
content: 'this is after';
background-color: var(--boxAfterBackColor,red);
position: absolute;
top: -20px;
right: 0px;
font-size: var(--boxAfterFontSize,20px);
}
JavaScript:
let box = document.getElementById('box');
box.style.setProperty('--boxAfterBackColor','green');
box.style.setProperty('--boxAfterFontSize','50px');
这对我有用,希望它也能帮到你。祝你好运!
在JavaScript中,有一种方法可以访问伪元素的值,而无需使用任何库。要获取该值,您需要使用“getComputedStyle”函数。第二个参数是可选的。
let elem = window.getComputedStyle(parent, ':before');
alert(elem.getPropertyValue('background'))
让 elem = window.getComputedStyle(document.querySelector('#item'), ':after'); console.log(elem.getPropertyValue('content'))
content
;请参见下面的示例以了解写模式。 - John