How to get a DOM element's ::before content with JavaScript?

后端 未结 2 848
旧时难觅i
旧时难觅i 2021-02-04 03:12

I want to know whether it is possible to get a DOM element\'s ::before content, which was set by CSS3.

I have tried some ways, but I still can\'t make it, s

相关标签:
2条回答
  • 2021-02-04 03:59

    Pass ":before" as the second parameter to window.getComputedStyle():

    console.log(getComputedStyle(document.querySelector('p'), ':before').getPropertyValue('content'));
    p::before,
    p::after {
      content: ' Test ';
    }
    <p>Lorem Ipsum</p>

    0 讨论(0)
  • 2021-02-04 04:00

    getComputedStyle() & getPropertyValue()

    image

    getComputedStyle(document.querySelector('span.search-box'), '::after').getPropertyValue('content');

    image

    0 讨论(0)
提交回复
热议问题