网站搜索

javascript - 属性 'pseudoStyle' 在类型 'HTMLElement' 上不存在


我正在尝试使用 TypeScript 修改伪元素的高度。

我在我的 IDE(vscode)中遇到以下错误

这是我的代码。

// select element
let el: HTMLElement = document.getElementById('filter-container');
//  style the height of psydo element
el.pseudoStyle("before", "height", newHeight);
...

答案#1

你不能只在某个特定元素上设置伪类样式,你可以通过修改样式表来实现:

document.styleSheets[0].insertRule('#id:hover { background-color: blue; }', 0);
document.styleSheets[0].cssRules[0].style.backgroundColor= 'blue';

答案#2

尝试 el.prototype.pseudoStyle("before", "height", newHeight);