原生js动态添加样式表

 
 

原生js给html动态添加样式表:

 
 
1.创建标签<style>添加一张内置样式表
            var style1 = document.createElement('style');
            style1.innerHTML = 'body{color:red}#top:hover{background-color: red;color: white;}';
            document.head.appendChild(style1);
2.另一种是添加外部样式表,即在文档中添加一个link节点,然后将href属性指向外部样式表的URL
            var link1 = document.createElement('link');
            link1.setAttribute('rel', 'stylesheet');
            link1.setAttribute('type', 'text/css');
            link1.setAttribute('href', 'reset-min.css');
            document.head.appendChild(link1);

猜你喜欢

转载自blog.csdn.net/fairyier/article/details/80196378