jQuery(三)_设置CSS

jQuery(三)_设置CSS

  • 获取css样式

    相当于js中计算后的样式

    getComputedStyle(box).width;

    box.currentStyle.width;

    $(".box").css("width");
    $(".box").css(["width","100px"]);
    //获取多个CSS样式,返回对象 
    
  • 设置div样式

    $("div").width(100);
    $("div").innerWidth(100);
    
    $("box1").offset({left:100,top:100});
    //设置到页面顶端距离位置
    
    方法名 描述
    innerWidth() width+padding
    innerHeight() height+padding
    outerWidth() width+padding+border
    outerHeight() height+padding+border
    outerWidth(true) width+padding+margin+border
    outerHeight(true) height+padding+margin+border
  • 设置div多个样式

    $("div").css({
        width:100,
        height:100,
        backgroundColor:"red"
    });
    $("div").css(["width","100px"])
    
  • 为多个div设置样式

    $("div").css("width",function(index){
        return 50 * (index + 1) + "px";
    })
    
  • 为div设置多个class样式

    $("div").addClass("box1");
    $("div").addClass("box1 box2");
    $("div").removeClass("box1");
    
  • 切换样式

    $(this).assClass("div1");
    $("box").hover(function(){
      $(this).addClass("div2");
    },function(){
      $(this).removeClass("div2");
    })
    
    $("div").addClass("div1").click(function () {
        $(this).toggleClass("div2");//开关切换
        $(this).toggleClass("div2", true);//仅切换一次
        $(this).toggleClass("div2", false);//不切换
    })
    
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 5,908评论 0 1
  • 一, call和apply call和apply的区别 二者都可以改变当前的this,区别在于apply方法要将...
    用户400826阅读 968评论 0 0
  • jquery介绍 jQuery是目前使用最广泛的javascript函数库。据统计,全世界排名前100万的网站,有...
    1263536889阅读 2,795评论 0 1
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,179评论 0 2
  • 《财富自由之路》 今天是精读营最后一天,我要问自己一个问题:开营前要解决的问题(升级操作系统)解决了吗? 人的大脑...
    宋大果儿阅读 1,117评论 0 1

友情链接更多精彩内容