操做对象 以及获取内存中的style

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">html

<html xmlns="http://www.w3.org/1999/xhtml">c++

<head>浏览器

<title>操做对象</title>函数

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />ui

<meta name="description" content="布尔教育 http://www.itbool.com" />spa

</head>xml

    <body>htm

     <div id="header" onclick="toggle()" style="width:500px;height:200px ;border-Bottom:solid 1px black;background:yellow">实现点击红绿交替</div>对象

    </body>ip

    <script>

    // var s=document.getElementById("header");

    // s.style.borderTop="solid 10px green";

    // // 注意这里使用的是去掉-而且把字母大写

    var c=1;

    function toggle(){


     var test=document.getElementById("header");

     // parseInt转化为整数

     var a=parseInt(test.style.width);

     var b=parseInt(test.style.height);

        test.style.borderBottomWidth=parseInt(test.style.borderBottomWidth)+2+"px";

     test.style.width=a+100+"px";

     test.style.height=b+100+"px";

     

     c++;

     if(c%2==0){

     test.style.background="red";

     }

     else test.style.background="blue";


    }

    </script>

</html>

/***************************/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>操做对象</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta name="description" content="布尔教育 http://www.itbool.com" />

</head>

    <body>

     <div id="header" onclick="toggle()" style="width:500px;height:200px ;border-Bottom:solid 1px black;background:yellow">实现点击红绿交替</div>

    </body>

    <script>

    // var s=document.getElementById("header");

    // s.style.borderTop="solid 10px green";

    // // 注意这里使用的是去掉-而且把字母大写

    //封装一个函数,获取当前内存中的style

    function getType(obj,attr){

        //为只读

            // obj.currentStyle[attr]:在IE中获取当前的style属性

            // window.getComputedStyle(obj,null)[attr];在其余浏览器中获取属性

            return obj.currentStyle?obj.currentStyle[attr]:window.getComputedStyle(obj,null)[attr];


    }

    var c=1;

    function toggle(){


     var test=document.getElementById("header");

     // parseInt转化为整数

     var a=parseInt(test.style.width);

     var b=parseInt(test.style.height);

        test.style.borderBottomWidth=parseInt(test.style.borderBottomWidth)+2+"px";

     test.style.width=a+100+"px";

     test.style.height=b+100+"px";

     alert(getType(test,'width'));

     c++;

     if(c%2==0){

     test.style.background="red";

     }

     else test.style.background="blue";


    }

    </script>

</html>

相关文章
相关标签/搜索