【问题标题】:Property 'pseudoStyle' does not exist on type 'HTMLElement'“HTMLElement”类型上不存在属性“pseudoStyle”
【发布时间】:2018-10-19 08:52:38
【问题描述】:

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

我的 IDE (vscode) 中出现以下错误

这是我的代码。

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

【问题讨论】:

  • HTMLElement 接口中没有 pseudoStyle 属性。这就是它在 IDE 中引发错误的原因。你可以这样做let el: any = document.getElementById('filter-container');
  • @BasavarajBhusani 我试过任何一个,它给出了错误 el.pseudoStyle is not a function
  • 你是在 VS 代码 IDE 中还是在运行时遇到这个错误?
  • @BasavarajBhusani 在运行时

标签: javascript angular typescript ionic3 pseudo-element


【解决方案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);

    【讨论】:

      【解决方案3】:

      据我所知,在 JavaScript 中我们无法更改伪元素,我猜在 TS 中也是如此,因为它是 JS 超集。 致以最诚挚的问候。

      【讨论】:

        猜你喜欢
        • 2022-01-13
        • 2021-12-15
        • 2019-09-16
        • 2017-08-08
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多