一、 arguments用法:
例:
function css(a,b,c){//调用方法使用的实参a.style[b]=c;
}
用arguments实现:
function css(){
arguments[0].style[argument[1]] = arguments[2];
}
优点:省略编写函数中的形参,用arguments数组存储调用方法时的实参。
二、解决js获取CSS属性值兼容性:
因为浏览器对于js函数方法的支持程度不一,所以编写函数时要考虑到兼容性问题,
<script type="text/javascript">
function getCss(obj,val){//封装兼容性获取CSS样式属性
if(getComputedStyle(obj,true)[val]){
alert(getComputedStyle(obj,true)[val]);
}else{
alert(obj.currentStyle[val]);
}
}
function css(a,b,c){
a.style[b]=c;
}
window.οnlοad=function(){
var box = document.getElementById("box");
getCss(box,'background-color');
//alert(css(box,"width"));
//css(box,'border','10px solid black');
//alert(getComputedStyle(box,true).width);//作用于非IE8以下浏览器获取CSS样式属性值
//alert(box.currentStyle.width);//只能作用于IE浏览器获取CSS样式属性值
}
</script>
三、object对象:
var obj = new Object();
=
var obj={};//创建一组空对象
四、用for语句遍历对象:
js for循环 遍历对象属性
var person = {fname:"John",lname:"Doe",age:25};
for (x in person) {
console.log(x + "=" + person[x]);
}
控制台输出结果:
fname=John
lname=Doe
age=25
js for循环 遍历数组
var mycars = new Array("Saab","Volvo","BMW");
for (x in mycars){
console.log(x + "=" + mycars[x])
}
控制台输出结果:
0=Saab
1=Volvo
2=BMW
五、数组中的方法:
concat() | 连接两个或更多的数组,并返回结果。 |
join() | 把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。 |
pop() | 删除并返回数组的最后一个元素 |
push() | 向数组的末尾添加一个或更多元素,并返回新的长度。 |
reverse() | 颠倒数组中元素的顺序。 |
shift() | 删除并返回数组的第一个元素 |
slice() | 从某个已有的数组返回选定的元素 |
sort() | 对数组的元素进行排序 |
splice() | 删除元素,并向数组添加新元素。 |
toSource() | 返回该对象的源代码。 |
toString() | 把数组转换为字符串,并返回结果。 |
toLocaleString() | 把数组转换为本地数组,并返回结果。 |
unshift() | 向数组的开头添加一个或更多元素,并返回新的长度。 |
valueOf() | 返回数组对象的原始值 |
var str ='border-bottom-color';
alert(str);
str=str.replace(/bottom/,"Bottom");//Bottom字符串替换原来的bottom字符串
charAt(index)返回字符串中下标为index的数据