zoukankan      html  css  js  c++  java
  • CSSText属性批量修改样式

     
    给一个HTML元素设置css属性
    • var head= document.getElementById("head");
      head.style.width = "200px";
      head.style.height = "70px";
      head.style.display = "block";
     
     
    使用cssText属性
    • var head= document.getElementById("head");
      head.style.cssText="200px;height:70px;display:bolck";
     
     

      cssText很快捷且所有浏览器都支持。此外当批量操作样式时,cssText只需一次reflow,提高了页面渲染性能。

      但cssText也有个缺点,会覆盖之前的样式。
     
     
      使用cssText时应该采用叠加的方式以保留原有的样式
    • function setStyle(el, strCss){
          var sty = el.style;
          sty.cssText = sty.cssText + strCss;
      }
     
     
      使用该方法在IE9/Firefox/Safari/Chrome/Opera中没什么问题,但由于 IE6/7/8中cssText返回值少了分号 会让你失望。因此对IE6/7/8还需单独处理下,如果cssText返回值没";"则补上
    • function setStyle(el, strCss){
          function endsWith(str, suffix) {
              var l = str.length - suffix.length;
              return l >= 0 && str.indexOf(suffix, l) == l;
          }
          var sty = el.style,
          cssText = sty.cssText;
          if(!endsWith(cssText, ';')){
              cssText += ';';
          }
          sty.cssText = cssText + strCss;
      }
     
     
     
     
     
  • 相关阅读:
    acwing2-01背包问题
    背包问题(转载)
    考研易错点 二叉树的度和图的度
    考研易错点*s++
    考研复习易错点数组指针和指针数组
    Android Crash Learning
    Mysql5.7中的分组排序
    康师傅JVM:StringTable(十三)
    RocketMQ集群搭建
    RocketMq的单机安装
  • 原文地址:https://www.cnblogs.com/10-19-92/p/5818914.html
Copyright © 2011-2022 走看看