元素样式操作包括了直接设置CSS 样式、增加CSS 类别、类别切换、删除类别这几种操作方法。
而在整个jQuery 使用频率上来看,CSS 样式的操作也是极高的,所以需要重点掌握。

 

 一、css()方法:

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
   $(function(){
      //alert($("#box").css("color"));//获取某个元素的行内样式,不同浏览器结果不一样,有些返回rgb格式的颜色值
      $("#box").css("color","yellow");//设置某个元素的行内样式
   });
</script>
<style type="text/css">
    #box{color:red}
</style>
</head>
<body>
    <div id="box" title="我是域名">发生</div>
</body>

  在CSS 获取上,我们也可以获取多个CSS 样式,而获取到的是一个对象数组,如果用传统方式进行解析需要使用for in 遍历。

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
   $(function(){
          var box = $('div').css(['color', 'height', 'width']); //得到多个CSS 样式的数组对象
        for (var i in box) { //逐个遍历出来
            alert(i + ':' + box[i]);
        }
   });
</script>
<style type="text/css">
    #box{color:red}
</style>
</head>
<body>
    <div id="box" title="我是域名">发生</div>
</body>  

  jQuery 提供了一个遍历工具专门来处理这种对象数组,$.each()方法,这个方法可以轻松的遍历对象数组。

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
   $(function(){
          var box = $('div').css(['color', 'height', 'width']); //得到多个CSS 样式的数组对象
        /*for (var i in box) { //逐个遍历出来
            alert(i + ':' + box[i]);
        }*/
        $.each(box, function (attr, value) { //遍历JavaScript 原生态的对象数组
            alert(attr + ':' + value);
        });
   });
</script>
<style type="text/css">
    #box{
        color:red;
        height:200px;
        width:300px;
    }
</style>
</head>
<body>
    <div id="box" title="我是域名">发生</div>
</body>

  使用$.each()可以遍历原生的JavaScript 对象数组,如果是jQuery 对象的数组怎么使用.each()方法呢?

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
   $(function(){
          var box = $('div').css(['color', 'height', 'width']); //得到多个CSS 样式的数组对象
        /*for (var i in box) { //逐个遍历出来
            alert(i + ':' + box[i]);
        }*/
        /*$.each(box, function (attr, value) { //遍历JavaScript 原生态的对象数组
            alert(attr + ':' + value);
        });*/
        $('div').each(function (index, element) { //index 为索引,element 为元素DOM
            alert(index + ':' + element);
        });
   });
</script>
<style type="text/css">
    #box{
        color:red;
        height:200px;
        width:300px;
    }
</style>
</head>
<body>
    <div id="box" title="我是域名">发生</div>
    <div id="" title="我是域名">发生</div>
    <div id="" title="我是域名">发生</div>
</body>

  在需要设置多个样式的时候,我们可以传递多个CSS 样式的键值对即可。

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
   $(function(){
          $('div').css({
            'background-color' : '#ccc',
            'color' : 'red',
            'font-size' : '20px'
        });
   });
</script>
<style type="text/css">
    #box{
        color:red;
        height:200px;
        width:300px;
    }
</style>
</head>
<body>
    <div id="box" title="我是域名">发生</div>
    <div id="" title="我是域名">发生</div>
    <div id="" title="我是域名">发生</div>
</body>

  如果想设置某个元素的CSS 样式的值,但这个值需要计算我们可以传递一个匿名函数。

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
   $(function(){
          $('#box').css('width', function (index, value) {
            return (parseInt(value) - 500) + 'px';    //value是之前的长度,这里将全局的操作包装成局部的操作
        });
   });
</script>
<style type="text/css">
    #box{
        color:red;
        height:200px;
        width:800px;
    }
</style>
</head>
<body>
    <div id="box" title="我是域名">发生</div>
    <div id="" title="我是域名">发生</div>
    <div id="" title="我是域名">发生</div>
</body>

二、addClass()方法/removeClass()方法

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
   $(function(){
          $('div').addClass("box");//添加一个类
        $("div").addClass("box bg");//同事添加多个类
   });
</script>
<style type="text/css">
    .box{
        color:red;
        height:20px;
        width:800px;
    }
    .bg{
        background-color:yellow;
    }
</style>
</head>
<body>
    <div id="" title="我是域名">发生</div>
    <div id="" title="我是域名">发生</div>
    <div id="" title="我是域名">发生</div>
</body>
<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
   $(function(){
          $('div').removeClass("box");//删除一个类
        $("div").removeClass("box bg");//同时删除多个类
   });
</script>
<style type="text/css">
    .box{
        color:red;
        height:20px;
        width:800px;
    }
    .bg{
        background-color:yellow;
    }
</style>
</head>
<body>
    <div class="box" title="我是域名">发生</div>
    <div class="box bg" title="我是域名">发生</div>
    <div class="box" title="我是域名">发生</div>
</body>

三、toggleClass()方法

  我们还可以结合事件来实现CSS 类的样式切换功能。

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
      $(function(){
         $('div').click(function(){ //当点击后触发
            $(this).toggleClass('box bg');     //单个样式多个样式均可
        });
    });
</script>
<style type="text/css">
    .box{
        color:red;
        height:20px;
        width:800px;
    }
    .bg{
        background-color:yellow;
    }
</style>
</head>
<body>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
</body>

  .toggleClass()方法的第二个参数可以传入一个布尔值,true 表示执行切换到class 类,false表示执行回默认class 类(默认的是空class),运用这个特性,我们可以设置切换的频率。

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
      $(function(){
        var count = 0;
        $('div').click(function () { //每点击两次切换一次red
            $(this).toggleClass('box bg', count++ % 3 == 0);
        });
    });
</script>
<style type="text/css">
    .box{
        color:red;
        height:20px;
        width:800px;
    }
    .bg{
        background-color:yellow;
    }
</style>
</head>
<body>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
</body>

  默认的CSS 类切换只能是无样式和指定样式之间的切换,如果想实现样式1 和样式2之间的切换还必须自己写一些逻辑。

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
      $(function(){
        $('div').click(function () {
            $(this).toggleClass('blue'); //一开始切换到样式2
            if ($(this).hasClass('blue')) { //判断样式2 存在后
                $(this).removeClass('red'); //删除样式1
            } else {
                $(this).toggleClass('red'); //添加样式1,这里也可以addClass
            }
        });
    });
</script>
<style type="text/css">
    .red{
        color:red;
    }
    .blue{
        color:blue;
    }
</style>
</head>
<body>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
</body>

  上面的方法较为繁琐,.toggleClass()方法提供了传递匿名函数的方式,来设置你所需要切换的规则。

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
      $(function(){
        $('div').click(function () {
            $(this).toggleClass(function () { //传递匿名函数,返回要切换的样式
                return $(this).hasClass('red') ? 'blue' : 'red';
            });
        });
    });
</script>
<style type="text/css">
    .red{
        color:red;
    }
    .blue{
        color:blue;
    }
</style>
</head>
<body>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
</body>

  注意:上面虽然一句话实现了这个功能,但还是有一些小缺陷,因为原来的class 类没有被删除,只不过被替代了而已。所以,需要改写一下。

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript">
      $(function(){
        $('div').click(function () {
            $(this).toggleClass(function () {
                if ($(this).hasClass('red')) {
                    $(this).removeClass('red');
                    return 'blue';
                } else {
                    $(this).removeClass('blue');
                    return 'red';
                }
            });
        });
    });
</script>
<style type="text/css">
    .red{
        color:red;
    }
    .blue{
        color:blue;
    }
</style>
</head>
<body>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
    <div class="" title="我是域名">发生</div>
</body>

 

JQuery_元素样式操作的更多相关文章

  1. JQuery_元素属性操作

    除了对元素内容进行设置和获取,通过jQuery 也可以对元素本身的属性进行操作,包括获取属性的属性值.设置属性的属性值,并且可以删除掉属性. <script type="text/ja ...

  2. jQuery编程基础精华02(属性、表单过滤器,元素的each,表单选择器,子元素过滤器(*),追加方法,节点,样式操作)

    属性.表单过滤器 属性过滤选择器: $("div[id]")选取有id属性的<div> $("div[title=test]")选取title属性为 ...

  3. jQuery学习之------元素样式的操作

    jQuery学习之------元素样式的操作 一..addClass( className )方法----增加样式 1.addClass( className ) : 为每个匹配元素所要增加的一个或多 ...

  4. DOM访问元素样式和操作元素样式

    在HTML中定义样式的方式有三种:通过<link/>元素包含外部样式表文件(外部样式表).使用<style/>元素定义嵌入式样式(嵌入式样式表).使用style特性定义针对特定 ...

  5. 解密jQuery内核 样式操作

    基础回顾 jQuery里节点样式读取以及设置都是通过.css()这个方法来实现的,本章通一下分解探究下jquery里这部分代码的实现 那么jQuery要处理样式的哪些问题? 先简单回顾下样式操作会遇到 ...

  6. jQuery 2.0.3 源码分析 样式操作

    根据API分类 CSS addClass() jQuery.cssHooks .hasClass() .removeClass() .toggleClass() .addClass() 对元素的样式操 ...

  7. 深入学习jQuery样式操作

    × 目录 [1]设置样式 [2]增加样式 [3]删除样式[4]切换样式[5]判断样式[6]样式操作 前面的话 使用javascript脚本化CSS是一个系列,包括行间样式.计算样式.CSS类.样式表. ...

  8. 11月8日上午Jquery的基础语法、选取元素、操作元素、加事件、挂事件及移除事件

    jquery基础知识 1.jquery文件的引入,所有的js代码要写在下面那段代码下面. <script src="../jquery-1.11.2.min.js">& ...

  9. JavaScipt 样式操作

    我们知道HTML样式定义的三种方式: <link/>外部引入也就是定义 CSS 中的 <style/>嵌入式样式 style特性地定义 给一个HTML元素设置css属性,如: ...

随机推荐

  1. js事件监听/鼠标滚轮/行为/冒泡/键盘的兼容性写法

    addEvent:function(el,type,fn,capture) { if (window.addEventListener) { if (type === "mousewheel ...

  2. HTML5--div、span超出部分省略号显示

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 选中统计winform

    private void gridControl1_MouseUp(object sender, MouseEventArgs e) { Dictionary<string, decimal&g ...

  4. win2012,oracle11g,sqlplus切换实例的方法

    问题环境:windows 2012 r2 64位  ,oracle 11.2.0.4,多个实例. 在这种情况下, sqlplus "/as sysdba" 默认登录的是系统后面安装 ...

  5. 优化 bulk insert

    https://www.simple-talk.com/sql/learn-sql-server/bulk-inserts-via-tsql-in-sql-server/

  6. sp_MSforeachtable 与 sp_MSforeachdb

    在MSSQL里有许多不公开的系统存储过程,其中可能常用的sp_MSforeachtable和sp_MSforeachdb有这2个.分别用于遍历某数据库的每个用户表.每个数据库. sp_MSforeac ...

  7. python学习笔记之常用模块(第五天)

    参考老师的博客: 金角:http://www.cnblogs.com/alex3714/articles/5161349.html 银角:http://www.cnblogs.com/wupeiqi/ ...

  8. 使用FastJSON,将对象或数组和JSON串互转

    Fastjson,是阿里巴巴提供的一个Java语言编写的高性能功能完善的JSON库.其开源的下载网址为:https://github.com/AlibabaTech/fastjson. 示例代码如下: ...

  9. Ajax.BeginForm()实现ajax无刷新提交

    1. 同时安装 Microsoft jQuery Unobtrusive ajax 和 jQuery Unobtrusive Ajax,如下图 安装完成之后多了如下的js库 2. 引用该js库 lay ...

  10. 关于Java加载属性文件放在web容器不好使的解决办法

    //使用Spring的工具就行了1 import java.util.Properties; import org.springframework.core.io.support.Properties ...