Why is document.querySelector not working on pseudo element?

后端 未结 1 516
刺人心
刺人心 2021-01-18 07:06

I am playing with pseudo elements and javascript but some how i can not style it using javascript.

1条回答
  •  無奈伤痛
    2021-01-18 08:06

    If you want to style pseudo elements from javascript you have to use the CSSOM to inject the rules. It's not trivial, but it's possible.

    var sheet = document.styleSheets[0]; //get style sheet somehow
    var rules = sheet.rules; 
    sheet.insertRule('div:before { display: none; }', rules.length);
    

    a slightly modified example from this article

    CCSOM Reference

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