【问题标题】:Is there way to docuement.getElementById('ELEMENT_ID') to change the content if it has a ::before in the css有没有办法 document.getElementById('ELEMENTID') 如果它在 css 中有 ::before 来更改内容
【发布时间】:2021-06-03 07:54:19
【问题描述】:

我需要使用 JavaScript 更改内容,但该元素在 css 中有 ::before

【问题讨论】:

  • 如果没有一些代码(HTML、JavaScript 和 CSS)以及对您正在尝试做什么和什么不起作用的详细描述,我们将无法为您提供帮助
  • 这个很有帮助,请检查,stackoverflow.com/questions/44342065/… 你可以使用它检查任何设置的 css 属性并添加条件。

标签: javascript css


【解决方案1】:

我假设你的 :before 伪元素有一个属性 content 如果它是可见的,那么你可以使用 .getComputedStyle() method

let style = window.getComputedStyle(document.querySelector('#content'),':before')

那么你必须检查你的“内容”属性

var content = style.getPropertyValue("content")

然后,您可以使用此内容值创建条件

if( content == 'something' ) {
    document.querySelector('#content').innerHTML = "your new content"
}

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 2015-03-05
    • 2013-11-02
    • 2013-10-15
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多