jQuery中样式和属性模块简单分析
1、行内样式操作
- 目标:扩展框架实现行内样式的增删改查
1.1 创建 css 方法
- 目标:实现单个样式或者多个样式的操作
1.1.1 css方法 -获取样式
- 注意:使用 style 属性只能获取行内样式
- 解释:获取类或者外部样式文件中设置的样式要使用
- W3C规范:window.getComputedStyle(dom)
- IE中 :dom.currentStyle
- itcast.fn.extend({
- css: function(name, value) {
- return window.getComputedStyle(this[0])[name];
- }
- });
1.1.2 css方法 -设置样式
- 解释:两个参数表示设置样式
- itcast.fn.extend({
- css: function(name, value) {
- if(value === undefined) {
- return window.getComputedStyle(this[0])[name];
- }
- return this.each(function() {
- this.style[name] = value;
- });
- }
- });
1.1.3 css方法 -对象字面量参数
- 解释:参数为对象字面量同时设置多个样式属性
- itcast.fn.extend({
- css: function(name, value) {
- if(value === undefined) {
- if(typeof name === "object") {
- return this.each(function() {
- for(var k in name) {
- this.style[k] = name[k];
- }
- });
- }
- return window.getComputedStyle(this[0])[name];
- } else {
- return this.each(function() {
- this.style[name] = value;
- });
- }
- }
- });
2、类操作
- 目标:扩展框架实现类样式的增删改查
2.1 hasClass方法 -判断有没有类
- 注意:判断匹配的所有元素中是否具有指定的类
- itcast.fn.extend({
- hasClass: function(cName) {
- var hasCName = false;
- // 判断第一个元素
- itcast.each(this[0].className.split(" "), function(i, v) {
- if(v === cName) {
- hasCName = true;
- return false;
- }
- });
- return hasCName;
- // 判断所有元素
- // this.each(function() {
- // hasCName = (" " + this.className + " ")
- // .indexOf( " " + trim(cName) + " ") !== -1;
- // if(hasCName) {
- // hasCName = true;
- // return false;
- // }
- // });
- // return hasCName;
- }
- });
2.2 addClass方法 -添加类
- itcast.fn.extend({
- addClass: function(cName) {
- return this.each(function() {
- var className = this.className;
- className += " " + cName;
- this.className = itcast.trim(className);
- });
- }
- });
2.3 removeClass -移除类
- itcast.fn.extend({
- removeClass: function(cName) {
- return this.each(function() {
- var cArr = this.className.split(" "),
- i, len = cArr.length;
- for(i = 0; i < len; i++) {
- if(cArr[i] === cName) {
- break;
- }
- }
- cArr.splice(i, 1);
- this.className = cArr.join(" ");
- // 2 replace 替换
- // var className = " " + this.className + " ";
- // this.className = itcast.trim(className.replace(" " + cName + " ", " "));
- // 处理多个相同类名的情况
- // var clsName = " " + this.className.trim() + " ";
- // while(clsName.indexOf(" " + name + " ") > -1) {
- // clsName = clsName.replace(" " + name + " ", " ");
- // }
- // this.className = clsName.trim();
- });
- }
- });
2.4 toggleClass -切换类
- itcast.fn.extend({
- toggleClass: function(cName) {
- if(this.hasClass(cName)) {
- this.removeClass(cName);
- } else {
- this.addClass(cName);
- }
- return this;
- }
- });
3、属性模块
3.1 演示jQuery中的相关方法
- 目标:复习jQuery中常用的属性操作方法
- 内容:attr / val / html / text
- 特点:
- 设置:给所有匹配到的元素设置
- 读取:只获取第一个元素的属性或内容
3.2 attr方法 -属性操作
- 注意:setAttribute 只能用来设置默认值。要访问或修改当前值,使用 elt[name] = value 代替
- itcast.fn.extend({
- attr: function(name, value) {
- if(value === undefined) {
- return this[0].getAttribute(name);
- }
- return this.each(function() {
- // this.setAttribute(name, value);
- this[name] = value;
- });
- }
- });
- 案例:随机切换图片
3.3 表单值操作
- itcast.fn.extend({
- val: function(value) {
- if(value === undefined) {
- return this[0].value;
- }
- return this.each(function() {
- this.value = value;
- });
- }
- });
3.4 html 和 text操作
3.4.1 html 操作
- itcast.fn.extend({
- html: function(html) {
- if(html === undefined) {
- return this[0].innerHTML;
- }
- return this.each(function() {
- this.innerHTML = html;
- });
- }
- });
3.4.2 text 操作
- 注意:innerText是非标准属性
- itcast.extend({
- getInnerText: function(dom) {
- var textArr;
- if(dom.innerText !== undefined) {
- return dom.innerText;
- }
- textArr = getNodeText(dom);
- return textArr.join("");
- function getNodeText(node) {
- var cNodes = node.childNodes,
- len = cNodes.length, i, cNode,
- arr = [];
- for(i = 0; i < len; i++) {
- cNode = cNodes[i];
- if(cNode.nodeType === 3) {
- arr.push(cNode.nodeValue);
- } else if(cNodes.nodeType === 1) {
- arr = arr.concat( getNodeText(cNode) );
- }
- }
- return arr;
- }
- },
- setInnerText: function(dom, str) {
- if("innerText" in dom) {
- dom.innerText = str;
- } else {
- dom.innerHTML = "";
- dom.appendChild( document.createTextNode(str) );
- }
- }
- });
- itcast.fn.extend({
- text: function(text) {
- if(text === undefined) {
- return itcast.getInnerText(this[0]);
- }
- return this.each(function() {
- itcast.setInnerText(this, text);
- });
- }
- });
jQuery中样式和属性模块简单分析的更多相关文章
- jquery中attr和prop的区别分析
这篇文章主要介绍了jquery中attr和prop的区别分析的相关资料,需要的朋友可以参考下 在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别 ...
- rocketmq中的NettyRemotingClient类的简单分析
rocketmq中的NettyRemotingClient类的简单分析 Bootstrap handler = this.bootstrap.group(this.eventLoopGroupWork ...
- java 中 “文件” 和 “流” 的简单分析
java 中 FIle 和 流的简单分析 File类 简单File 常用方法 创建一个File 对象,检验文件是否存在,若不存在就创建,然后对File的类的这部分操作进行演示,如文件的名称.大小等 / ...
- DELPHI中完成端口(IOCP)的简单分析(4)
DELPHI中完成端口(IOCP)的简单分析(4) 在我以前写的文章中,一直说的是如何接收数据.但是对于如何发送数据却一点也没有提到.因为从代码量上来说接收的代码要比发送多很多.今天我就来写一下如 ...
- DELPHI中完成端口(IOCP)的简单分析(3)
DELPHI中完成端口(IOCP)的简单分析(3) fxh7622关注4人评论7366人阅读2007-01-17 11:18:24 最近太忙,所以没有机会来写IOCP的后续文章.今天好不容易有 ...
- DELPHI中完成端口(IOCP)的简单分析(2)
DELPHI中完成端口(IOCP)的简单分析(2) 今天我写一下关于DELPHI编写完成端口(IOCP)的工作者线程中的东西.希望各位能提出批评意见.上次我写了关于常见IOCP的代码,对于IOCP ...
- DELPHI中完成端口(IOCP)的简单分析(1)
DELPHI中完成端口(IOCP)的简单分析(1) 用DELPHI开发网络代码已经有一段时间了! 我发现在网上用VC来实现完成端口(IOCP)的代码很多,但是使用DELPHI来实现的就比较少了.对 ...
- jQuery - 02. 样式表属性操作/类操作、动画、显示隐藏、滑入、淡入、停止动画、节点操作、添加对象、清空节点
样式表属性操作.css $("div").css({'width':100,'height':100,'background':'red'}); $("div" ...
- JQuery中常用的 属性选择器
jQuery中使用$()作为选择符极大提高工作效率以及方便快捷;一些常用属性的选择器由以下几种 1) $('#id') id选择器 2) $('.class') css选择器,class类名 3) $ ...
随机推荐
- 学习网址Collect
Laravel 学院 https://laravelacademy.org/wx小程序 https://developers.weixin.qq.com/miniprogram/dev/quic ...
- SSM项目中表单分页操作(PageHepler使用)
Maven pom.xml添加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifa ...
- DFS-BFS深度优选遍历和广度优先遍历
https://www.jianshu.com/p/b086986969e6 DFS--需要借助stack实现 stack.push stack.pop BFS--需要借助队列queue stack- ...
- php第五节课
封装 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- [CodeForces]1042D
大意:求一个序列有几个子序列的和小于给定值,里面的数有正有负,序列长度≤200000. 列个式子,其实求的是sum[r]-sum[l-1]<T sum[r]-T<sum[l-1] 所以我们 ...
- 使用yum update更新文件系统时不更新内核的方法
CentOS使用yum update更新时不升级内核 cp /etc/yum.conf /etc/yum.confbak 方法一.修改yum的配置文件 vi /etc/yum.conf 在[m ...
- composer 安装教程
https://getcomposer.org/download/ 邓士鹏 1.先检查php.ini是否开启ssl ;extension=php_openssl.dll 2. php -r &qu ...
- 读取com口接收byte数据的处理
procedure Tfrm_CheckCloth.cnrs232ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); ...
- poj 1466 最大独立集
#include<stdio.h> #include<string.h>//这个分开后男的站在一边女的站在一边,不肯能有les或者gay.最大独立集=n-最大匹配数 #defi ...
- Solr Update插件自定义Update Chain按条件更新索引
背景:基于call客,来电和跟进记录等多个数据来源的用户文档,需要在更新是判断首来源的时间. 如对电话号码11xxxx来说,来电时间是今天,call客时间是昨天,而call客数据又可能因为网络原因晚上 ...