zoukankan      html  css  js  c++  java
  • JS之DOM篇动态样式

    动态样式是指页面加载过程中样式并不存在,页面加载完成后动态添加到页面的样式。动态样式包括通过<link>插入的外部样式表和通过<style>插入的内部样式两种

    外部样式

    <div class="box">测试文字</div>
    <button id="btn">动态添加样式</button>
    <script>
    function loadStyles(url){
      loadStyles.mark = 'load';
      var link = document.createElement('link');
      link.rel = 'stylesheet';
      link.href = url;
      var head = document.head;
      head.appendChild(link); 
    }
    btn.onclick = function(){
      if(loadStyles.mark != 'load'){
        loadStyles('./style.css');        
      }
    }
    </script>
    

    内部样式

    <div class="box">测试文字</div>
    <button id="btn">动态添加样式</button>
    <script>
    function loadStyles(str){
      loadStyles.mark = 'load';
      var style = document.createElement('style');
      style.innerHTML = str;
      var head = document.head;
      head.appendChild(style); 
    }
    btn.onclick = function(){
      if(loadStyles.mark != 'load'){
        loadStyles('.box{height:100px;100px;background-color: pink;}');     
      }
    }
    </script>
    
    优秀文章首发于聚享小站,欢迎关注!
  • 相关阅读:
    Electio Time poj
    排列的字典序问题
    poj 2365
    编程中的命名设计那点事(转)
    编程命名中的7+1个提示(转)
    poj 1664 放苹果——递归
    再论字典序
    poj 3618
    sort用法
    poj 1088
  • 原文地址:https://www.cnblogs.com/yesyes/p/15352419.html
Copyright © 2011-2022 走看看