javascript、jQuery动态修改css、属性

/*  javascript方法

*javascript动态修改css效果的方法(四种)

* 第一种:使用obj.className来修改样式表的类名。例如:div1.style.width="100px";

* 第二种:使用obj.style.cssText来修改嵌入式的css。例:div1.style.cssText="width:100px;height:100px;background: palevioletred;";

* 第三种:使用obj.className来修改样式表的类名。例如:div1.setAttribute("class","div2")

* 第四种:使用更改外联的css文件,从而改变元素的css。 例如:div1.setAttribute("href","css2.css");

*/

html代码:

<link href="css/css1.css" rel="stylesheet" id="cssLink" />
<div id="divBtn1" οnclick="changeCss1()">1</div>
<div id="divBtn2" οnclick="changeCss2()">2</div>
<div id="divBtn3" οnclick="changeCss3()">3</div>
<div id="divBtn4" οnclick="changeCss4()">4</div>

css1.css文件

复制代码

@charset "utf-8";
#divBtn1,#divBtn2,#divBtn3,#divBtn4{
    width:100px;
    height:100px;
    margin-bottom: 10px;
}
#divBtn1{background:yellowgreen;}
#divBtn2{background:paleturquoise;}
#divBtn3{border:1px solid #ccc}
#divBtn4{background:blue;}
.div3{width:100px;height:100px;background:blueviolet}

复制代码

css2.css文件

复制代码

@charset "utf-8";
#divBtn4{background: greenyellow;}
#divBtn1,#divBtn2,#divBtn3,#divBtn4{
    width:200px;
    height:200px;
    border:1px solid #ccc;
    margin-bottom: 10px;
}
#divBtn1{background:yellowgreen;}
#divBtn2{background:paleturquoise;}
.div3{width:100px;height:100px;background:blueviolet}

复制代码

js代码:

复制代码

<script>
            /*
             *javascript动态修改css效果的方法(四种) 
             * 第一种:div1.style.width="100px";
             * 第二种:div2.style.cssText="width:100px;height:100px;background: palevioletred;";
             * 第三种:div1.setAttribute("class","div2")和div3.className="div3";//效果一样
             * 第四种:使用更改外联的css文件,从而改变元素的css
             * obj.setAttribute("href","css/css2.css");
             * */
            function changeCss1(){
                var div1=document.getElementById("divBtn1");
                div1.style.width="100px";
                div1.style.height="100px";
                div1.style.background="red";
            }
            function changeCss2(){
                var div2=document.getElementById("divBtn2");
                div2.style.cssText="width:100px;height:100px;background: palevioletred;";
                //cssText会覆盖之前的设置  无兼容性问题  写法和css样式表相同
            }
            function changeCss3(){
                var div3=document.getElementById("divBtn3");
                //div3.className="div3";//效果一样
                div3.setAttribute("class","div3");
            }
            function changeCss4(){
                var obj=document.getElementById("cssLink");
                obj.setAttribute("href","css/css2.css");
            }
        </script>

复制代码

jQuery方法:

css部分:

复制代码

.div1{
      width:100px;
      height:100px;
      background:#00ff00;
      }
.div2{
       width:100px;
       height:100px;
       background:#ff0000;
      }
#Btndiv{
         width:100px;
         height:100px;
         border: 1px solid #ccc;
         margin-bottom: 10px;
         }        

复制代码

jQuery代码:

复制代码

       <div id="Btndiv" οnclick="changeCss()"></div>
        <script>
            $(document).ready(function (){
                //第一种
//                $("div").css("width","100px");
//                $("div").css("height","100px");
//                $("div").css("background","cyan");
                //第二种
//                $("div").css({
//                    width:"100px",height:"100px",background:"red"
//                });
                //第三种
                $("div").addClass("div1");
                $("div").click(function (){
//                    $(this).addClass("div2");
//                  $(this).removeClass("div1");
                    $(this).toggleClass("div2");
                });
            });
    </script>

复制代码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

正在修炼的IT大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值