js对样式的操作

 

本文有:对某个事件的来回操作实现对css样式的来回修改 。比如实现hover效果

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            #box{
                width: 200px;
                height: 200px;
                background-color: red;
            }
        </style>
    </head>
    <body>
        <div id="box">魔降风云变</div>
        <script type="text/javascript">
            // 1.获取事件源对象
            // var box = document.getElementById('box');

            // 2.绑定事件
            /* box.onmouseover = function (){
                // 3.让标签的背景色变绿

                box.style.backgroundColor = 'green';
                box.style.fontSize = '30px';


            }
            box.onmouseout = function (){
                // 3.让标签的背景色变绿

                box.style.backgroundColor = 'red';
                box.style.fontSize = '16px';

            } */
            // var isRed = true;
            // box.onclick = function(){
            //     if(isRed){
            //         this.style.backgroundColor = 'green';
            //         isRed = false;
            //     }else{
            //         this.style.backgroundColor = 'red';
            //         isRed = true;
            //     }

            // }

        </script>
    </body>
</html>

 

1、

1.1

 没有js的时候

 1.2

            // 1.获取事件源对象
            var box = document.getElementById('box');

            // 2.绑定事件
             box.onmouseover = function (){
                // 3.让标签的背景色变绿

                box.style.backgroundColor = 'green';
                box.style.fontSize = '30px';


            }

鼠标经过图,让图变绿,字体变大。使用js修改css样式

1)获取事件源,事件源就是要修改css的标签代码段;

 2)绑定事件 对象.onmouseover =function(){} 匿名函数。

 3)匿名函数中对象.样式.属性=‘新值’ ,修改颜色和字体。属性带-的去掉-并将-后第一个字母大写。驼峰体。

 

上面的改变就回不去了,只有刷新网页才能变回原来的样子。

1.3、两段代码js实现伪类选择器hover的效果

            // 1.获取事件源对象
            var box = document.getElementById('box');

            // 2.绑定事件
             box.onmouseover = function (){
                // 3.让标签的背景色变绿

                box.style.backgroundColor = 'green';
                box.style.fontSize = '30px';


            }
            box.onmouseout = function (){
                // 3.让标签的背景色变绿

                box.style.backgroundColor = 'red';
                box.style.fontSize = '16px';

            }

鼠标未覆盖

鼠标覆盖,能实现不断的切换了。

1.4、但是这里有代码冗余。修改一下,加个标志,

var box = document.getElementById('box');
            var isRed = true;
            box.onclick = function(){
                if(isRed){
                    this.style.backgroundColor = 'green';
                    isRed = false;
                }else{
                    this.style.backgroundColor = 'red';
                    isRed = true;
                }

            }

 

未点击:

点击。能实现每次点击实现颜色切换

 

转载于:https://www.cnblogs.com/machangwei-8/p/10976251.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值