JQuery_元素样式操作
元素样式操作包括了直接设置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_元素样式操作的更多相关文章
- JQuery_元素属性操作
除了对元素内容进行设置和获取,通过jQuery 也可以对元素本身的属性进行操作,包括获取属性的属性值.设置属性的属性值,并且可以删除掉属性. <script type="text/ja ...
- jQuery编程基础精华02(属性、表单过滤器,元素的each,表单选择器,子元素过滤器(*),追加方法,节点,样式操作)
属性.表单过滤器 属性过滤选择器: $("div[id]")选取有id属性的<div> $("div[title=test]")选取title属性为 ...
- jQuery学习之------元素样式的操作
jQuery学习之------元素样式的操作 一..addClass( className )方法----增加样式 1.addClass( className ) : 为每个匹配元素所要增加的一个或多 ...
- DOM访问元素样式和操作元素样式
在HTML中定义样式的方式有三种:通过<link/>元素包含外部样式表文件(外部样式表).使用<style/>元素定义嵌入式样式(嵌入式样式表).使用style特性定义针对特定 ...
- 解密jQuery内核 样式操作
基础回顾 jQuery里节点样式读取以及设置都是通过.css()这个方法来实现的,本章通一下分解探究下jquery里这部分代码的实现 那么jQuery要处理样式的哪些问题? 先简单回顾下样式操作会遇到 ...
- jQuery 2.0.3 源码分析 样式操作
根据API分类 CSS addClass() jQuery.cssHooks .hasClass() .removeClass() .toggleClass() .addClass() 对元素的样式操 ...
- 深入学习jQuery样式操作
× 目录 [1]设置样式 [2]增加样式 [3]删除样式[4]切换样式[5]判断样式[6]样式操作 前面的话 使用javascript脚本化CSS是一个系列,包括行间样式.计算样式.CSS类.样式表. ...
- 11月8日上午Jquery的基础语法、选取元素、操作元素、加事件、挂事件及移除事件
jquery基础知识 1.jquery文件的引入,所有的js代码要写在下面那段代码下面. <script src="../jquery-1.11.2.min.js">& ...
- JavaScipt 样式操作
我们知道HTML样式定义的三种方式: <link/>外部引入也就是定义 CSS 中的 <style/>嵌入式样式 style特性地定义 给一个HTML元素设置css属性,如: ...
随机推荐
- Android--自定义加载框
1,在网上看了下好看的加载框,看了一下,挺好看的,再看了下源码,就是纯paint画出来的,再加上属性动画就搞定了 再来看一下我们的源码 LvGhost.java package com.qianmo. ...
- Linux主机规划
当你想装linux操作系统的时候,一定要知道你的用途,不同的用途就要规划不同的装机方式. 首先要知道一些概念: 挂载:利用一个目录当成进入点,将磁盘分区的数据放置在该目录下. 磁盘第一个扇区记录的信息 ...
- linux安全运维之谁动了chattr
安全一直是老生常谈的问题,今天我们来谈谈chattr. 如果涉及到侵权问题:请联系w18030432178@outlook.com,我会尽快删除帖子 目录 0.chattr的简介 0.0 chattr ...
- 简单封装数据请求(iOS)
#import <Foundation/Foundation.h> //给block起 别名 //类型 void(^)(BOOL success , id data) //别名是 Comp ...
- html与Android——webView
1 <html> 2 <head> 3 <title>myHtml.html</title> 4 5 <meta http-equiv=" ...
- Linux中的命令 make -f 是什么意思
出处:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422460 ...
- wex5 实战 省市县三级联动与地址薄同步
无论是商城,还是快递,都要用到省市县三级联动,和地址薄,今天就以实战来制作,难点有3个: 1:三级联动,有wex5组件实现,相对简单,实战里对行数据进行了拼接 2: 地址薄选项,利用inputSel ...
- PHP 将下划线命名 转换为 驼峰式命名
function convertUnderline($str , $ucfirst = true) { $str = ucwords(str_replace('_', ' ', $str)); $st ...
- CentOS7 Nginx负载均衡
五台服务器 192.168.155.129 nginx反向代理服务器 192.168.155.130 apache+PHP服务器,PHP要使用mysql函数库,配置的时候就要指定mysql安装路径,所 ...
- String split
这个方法看似简单,其实如果使用不当,会出现很多问题 System.out.println(":ab:cd:ef::".split(":").length);// ...