假设 input
元素有伪元素 ::after
在该元素的末尾,然后有一个按钮,即有伪元素 ::after
之间 input
元素和 button
元素。例如,请看下面的图片-
在上面的例子中,当 button
单击后,一段时间后,input
已更改,::after
伪元素消失了。
如何检测输入有 ::after
还是不?
最佳答案
您可以使用 getComputedStyle(el, '::after').content
获取 ::after
伪元素的内容(适用于 ::之前
也是如此)。如果内容不是none
,则伪元素存在。
const hasAfter = selector => {
const el = document.querySelector(selector);
return getComputedStyle(el, '::after').content !== 'none';
}
console.log(hasAfter('.has')); // true
console.log(hasAfter('.hasnt')); // false
.has::after {
content: 'after';
}
<p class="has">has </p>
<p class="hasnt">hasn't</p>
关于javascript - 如何检测元素是否具有::after(伪元素)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58197145/