元素样式操作包括了直接设置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. mysql 设置max_allowed_packet 大小的办法

        show VARIABLES like '%max_allowed_packet%'; 第一句是查询  max_allowed_packet  的大小,第二句是重新设定  max_allowe ...

  2. iOS tableView 滚动后回到顶部

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint contentOffsetPoint = self.tableView ...

  3. Linux_用户级_常用命令(5):rm

    Linux常用命令第5集包含命令:rm 开篇语:懒是人类进步的源动力 本文原创,专为光荣之路公众号所有,欢迎转发,但转发请务必写出处! 一.命令简介 删除一个目录中的一个或多个文件或目录,如果没有使用 ...

  4. C++模拟实现JDK中的ArrayList和LinkedList

    Java实现ArrayList和LinkedList的方式采用的是数组和链表.以下是用C++代码的模拟: 声明Collection接口: #ifndef COLLECTION_H_ #define C ...

  5. HDU 2732:Leapin' Lizards(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:给出两个地图,蜥蜴从一个柱子跳跃到另外一个地方,那么这个柱子就可能会坍塌,第一个地图是柱子可以容忍跳 ...

  6. android ViewPager使用遇到的问题

    项目需求是需要实现一个有两页可滑动的界面,就想到了使用ViewPager,在实现是没有深入考虑,就直接使用了PagerAdapter,页面是正常实现了,可是发现无法流畅的刷新页面(直接使用notify ...

  7. MySQl查询区分大小写的解决办法

    通过查询资料发现需要设置collate(校对) . collate规则: *_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的 *_cs: ca ...

  8. java.sql.SQLException: null, message from server: "Host '192.168.xxx.xxx' is not allowed to connect to this MySQL server"

    当你连接自己的电脑上的MySQL时,报这样的错,你可以把ip换成 127.0.0.1或者localhost  ,当然前提是用户名和密码正确

  9. Codeforces Round #342 (Div. 2)-A. Guest From the Past

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  10. Java 枚举类

    如果要定义一个枚举类: public enum Size { SAMLL, MEDIUM, LARGE, EXTRA, EXTRA_LARGE}; 实际上,这个声明定义的类型是一个类,它刚好有4个实例 ...