本文主要和大家分享可变参数css函数获取非行间样式的方法,希望能帮助到大家。
1、可变参数
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>可变参数</title>
</head>
<script>
window.onload= function(){
function sum1(){
var sum=0; for(var i=0;i<arguments.length;i++){
sum+=arguments[i];
} return sum;
}
alert(sum1(1,2,3,4,5,6,7));
} </script>
<body>
</body></html>
2、css函数
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>css函数</title>
<script>
window.onload=function(){
//css1()和css2()是两个函数,但是功能是相同的。为了防止arguments出现太多容易混乱。所以给他们起一个名字
function css1(){
if(arguments.length==2){ //记得填写return,这样外部才能接收
return arguments[0].style[arguments[1]];
}else{ arguments[0].style[arguments[1]]=arguments[2];
}
} function css2(obj,name,value){
if(arguments.length==2){ //记得填写return,这样外部才能接收
return obj.style[name];
}else{
obj.style[name]=value;
}
} var op = document.getelementbyid('op');
alert(css2(op,'width')); //里面使用的是字符串
css2(op,'background','green');
} </script>
</head>
<body>
<p id='op' style="width: 100px;height: 100px;background: red;">
</p>
</body></html>
3、获取非行间样式
使用window.onload()时,里面不能嵌套别的函数,但是可以调用别的函数。
获取非行间样式有两种方法。
一是:currentstyle使用方法和style相同,但是只有ie兼容,
二是:getcomputedstyle,使用方法是getcomputedstyle(op,false).width;其中第一个参数是要获取的对象的名称,第二个参数是一个随意的值。
取行间样式时,不能够使用getcomputedstyle来取复合样式,比如background,但是如果想要取背景颜色,可以使用backgroundcolor
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>获取非行间样式</title>
<style>
#p1{width: 300px; height: 200px; background: red;}
</style>
<script>
window.onload=function(){
huoqu();
} function huoqu(){
var op = document.getelementbyid('p1'); if(op.currentstyle){
alert(op.currentstyle.width);
}else{
alert(getcomputedstyle(op,false).width);
}
} </script>
</head>
<body>
<p id='p1'>
</p>
</body> </html>
相关推荐:
如何获取非行间样式?利用js和jquery获取非行间样式
js获取非行间样式及兼容问题_html/css_web-itnose
关于css非行间的style用法,求解~_html/css_web-itnose
以上就是可变参数css函数获取非行间样式的方法的详细内容。