搜索
您的当前位置:首页正文

原生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);

因篇幅问题不能全部显示,请点此查看更多更全内容

Top