javascript - How to implement JS to dynamically modify css global styles
阿神
阿神 2017-05-19 10:11:38
0
3
656

If you want to dynamically modify the css global style in the page, for example, change the color of all fonts on the page to red
This can be achieved through css

*{color:red;}

And if you use js to modify the styles of all elements on the page, that is, click the button through js to dynamically modify the styles of all elements on the page to red, how should you implement it

阿神
阿神

闭关修行中......

reply all(3)
阿神

I can only think of this method.

Array.prototype.slice.call(document.all).forEach(function (ele) {
    ele.style.color = 'red';
});
阿神

Use css style-related interfaces stylesheet.insertRule或者stylesheet.addRule Both of these can dynamically insert css styles. The compatibility is good with ie9+

For example

    // https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule
    myStyle.insertRule("#blanc { color: white }", 0);

When you need to delete, there are deleteRuleremoveRuletwo methods. You can check the relevant information

phpcn_u1582

[].forEach.call(document.querySelectorAll('*'),function(a){
a.style.color = 'red';
})

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template