首页 技术 正文
技术 2022年11月21日
0 收藏 690 点赞 4,942 浏览 1938 个字

用js的style属性可以获得html标签的样式,但是不能获取非行间样式。

解决方法:

在IE下可以用currentStyle;

在FF下用getComputedStyle;

然而,为了让其兼容,解决方法,封装成getStyle事件:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#div2{width:500px;height:100px;background-color:green;}
</style>
</head>
<body>
<div id="div2"></div>
</body>
</html>
<script>
最初:
if(oDiv2.currentStyle){
//IE
console.log(oDiv2.currentStyle.height);
}else{
// FF
console.log(getComputedStyle(oDiv2,false).width);
}
   //封装一个兼容性的获取元素样式的函数
//分析:哪个元素,哪个样式
function getStyle(obj,attr){
if(obj.currentStyle){
      //IE
return obj.currentStyle[attr];
}else{
      //FF
return getComputedStyle(obj,false)[attr];
}
}
//用法
window.onload = function(){
var oDiv=document.getElementById("div2");
console.log(getStyle(oDiv,"width"));
}//进一步封装 //obj:获取谁的样式,attr:样式名称,value样式的值
function css(obj,attr,value){
if(arguments.length==2){//获取
return getStyle(obj,attr)
}else if(arguments.length == 3){//设置
obj.style[attr] =value;
}
}
//用法:
//      oBtn.onclick = function () {
// css(oDiv,"background","black");
// css(oDiv,"border","3px solid yellow");
// console.log(css(oDiv,"width"));
// }
</script>补充:
JavaScript中,函数本身的length属性和arguments.length到底有什么区别?
1,函数本身有length属性,表示参数的个数。
arguments.length也表示参数的个数。2,函数对象的length属性是形式参数的个数;
arguments伪变量的length属性是某次调用的实际参数的个数。
  例如:
    function func(a,b,c){
      console.log(arguments.length);//输出:3
    }
      console.log(func.length)//输出: 1
    func(1);
以上是后来发现错的,2017-03-16更正了以后:
      function func(a,b,c){
    console.log("arguments:"+arguments.length); //输出 arguments::1
    }
    console.log("length:"+func.length); //输出 length: 3
    func(1);

3,函数本身也是对象,对象就有属性,函数有length属性,比如: function fn(x,y,z) {}中 fn.length=3,说明了函数的形参个数; 而在函数体内,arguments.length表示传入函数的实参个数,比如:function fun(1,2) { console.log(arguments.length)} 中实参的个数为arguments.length=2

4,fn.length: 形参个数
arguments.length: 实参个数

//拓展:

//arguments  数组(所有的参数)  可变参(不定参)
function sum2() {
var result = 0;
var i = 0;
for(i=0;i<arguments.length;i++){
result += arguments[i];
}
console.log(result);//输出13}
sum2(4,4,5);

 总结,除了可以用arguments去判断参数个数,还可以做不定参的运算。

    

    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,964
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781