zoukankan      html  css  js  c++  java
  • js基础之arguments、css

    arguments就是一个包含传入的参数的数组对象

    栗子一:

    function sum(){
      var result=0;
      for(var i=0;i<arguments.length;i++){
        result += arguments[i];
      }return result;
    }

    alert(sum(1,2,3,4,5));

    css函数

    原代码:

    function css(){
      if(arguments.length==2){//获取
        //return arguments[0].style[arguments[1]];//undefined因为style只能取行间样式
        //return arguments[0].currentStyle.arguments[1];
          if(arguments[0].currentStyle){//做兼容性判断:判断特性比判断浏览器版本更高效
          //IE
            return arguments[0].currentStyle[arguments[1]];//currentStyle为计算后的样式或默认值(如font-size)
        } else {
          //ff/chrome
            return getComputedStyle(arguments[0])[arguments[1]];//getComputedStyle为计算后的样式或默认值(如font-size)

        }
      }else if(arguments.length==3){//设置
          arguments[0].style[arguments[1]]=value;
      }
    }
    window.onload=function(){
      var obtn = document.getElementById('btn1');
      var odiv = document.getElementById('div1');

      obtn.onclick=function(){
        css(odiv,'background','#999');
        alert(css(odiv,'width'));
      }
    }

     

    增强可读性后的代码:

    function css(obj,attr,value){
      if(arguments.length==2){//获取
        //return arguments[0].style[arguments[1]];//undefined因为style只能取行间样式
        //return arguments[0].currentStyle.arguments[1];
        if(obj.currentStyle){//做兼容性判断:判断特性比判断浏览器版本更高效
          //IE
          return obj.currentStyle[attr];//currentStyle为计算后的样式或默认值(如font-size)
        } else {
          //ff/chrome
          return getComputedStyle(obj)[attr];//getComputedStyle为计算后的样式或默认值(如font-size)

        }
      }else if(arguments.length==3){//设置
        obj.style[attr]=value;
      }
    }


    作者:狂流
    出处:http://www.cnblogs.com/kuangliu/
    欢迎转载,分享快乐! 如果觉得这篇文章对你有用,请抖抖小手,推荐一下!

  • 相关阅读:
    Intent
    What should we do next in general after collecting relevant data
    NOTE FOR Secure Friend Discovery in Mobile Social Networks
    missing pcap.h
    after building Android Source code
    plot point(one column)
    When talking to someone else, don't infer that is has been talked with others at first. It may bring repulsion to the person who is talking with you.
    进程基本知识
    Python input和raw_input的区别
    强制 code review:reviewboard+svn 的方案
  • 原文地址:https://www.cnblogs.com/kuangliu/p/3473802.html
Copyright © 2011-2022 走看看