1、行内样式操作

  • 目标:扩展框架实现行内样式的增删改查

1.1 创建 css 方法

  • 目标:实现单个样式或者多个样式的操作

1.1.1 css方法 -获取样式

  • 注意:使用 style 属性只能获取行内样式
  • 解释:获取类或者外部样式文件中设置的样式要使用
    • W3C规范:window.getComputedStyle(dom)
    • IE中 :dom.currentStyle
  1. itcast.fn.extend({
  2. css: function(name, value) {
  3. return window.getComputedStyle(this[0])[name];
  4. }
  5. });

1.1.2 css方法 -设置样式

  • 解释:两个参数表示设置样式
  1. itcast.fn.extend({
  2. css: function(name, value) {
  3. if(value === undefined) {
  4. return window.getComputedStyle(this[0])[name];
  5. }
  6.  
  7. return this.each(function() {
  8. this.style[name] = value;
  9. });
  10. }
  11. });

1.1.3 css方法 -对象字面量参数

  • 解释:参数为对象字面量同时设置多个样式属性
  1. itcast.fn.extend({
  2. css: function(name, value) {
  3. if(value === undefined) {
  4. if(typeof name === "object") {
  5. return this.each(function() {
  6. for(var k in name) {
  7. this.style[k] = name[k];
  8. }
  9. });
  10. }
  11.  
  12. return window.getComputedStyle(this[0])[name];
  13. } else {
  14. return this.each(function() {
  15. this.style[name] = value;
  16. });
  17. }
  18. }
  19. });

2、类操作

  • 目标:扩展框架实现类样式的增删改查

2.1 hasClass方法 -判断有没有类

  • 注意:判断匹配的所有元素中是否具有指定的类
  1. itcast.fn.extend({
  2. hasClass: function(cName) {
  3. var hasCName = false;
  4. // 判断第一个元素
  5. itcast.each(this[0].className.split(" "), function(i, v) {
  6. if(v === cName) {
  7. hasCName = true;
  8. return false;
  9. }
  10. });
  11. return hasCName;
  12.  
  13. // 判断所有元素
  14. // this.each(function() {
  15. // hasCName = (" " + this.className + " ")
  16. // .indexOf( " " + trim(cName) + " ") !== -1;
  17. // if(hasCName) {
  18. // hasCName = true;
  19. // return false;
  20. // }
  21. // });
  22. // return hasCName;
  23. }
  24. });

2.2 addClass方法 -添加类

  1. itcast.fn.extend({
  2. addClass: function(cName) {
  3. return this.each(function() {
  4. var className = this.className;
  5. className += " " + cName;
  6. this.className = itcast.trim(className);
  7. });
  8. }
  9. });

2.3 removeClass -移除类

  1. itcast.fn.extend({
  2. removeClass: function(cName) {
  3. return this.each(function() {
  4. var cArr = this.className.split(" "),
  5. i, len = cArr.length;
  6.  
  7. for(i = 0; i < len; i++) {
  8. if(cArr[i] === cName) {
  9. break;
  10. }
  11. }
  12.  
  13. cArr.splice(i, 1);
  14. this.className = cArr.join(" ");
  15.  
  16. // 2 replace 替换
  17. // var className = " " + this.className + " ";
  18. // this.className = itcast.trim(className.replace(" " + cName + " ", " "));
  19.  
  20. // 处理多个相同类名的情况
  21. // var clsName = " " + this.className.trim() + " ";
  22. // while(clsName.indexOf(" " + name + " ") > -1) {
  23. // clsName = clsName.replace(" " + name + " ", " ");
  24. // }
  25. // this.className = clsName.trim();
  26. });
  27. }
  28. });

2.4 toggleClass -切换类

  1. itcast.fn.extend({
  2. toggleClass: function(cName) {
  3. if(this.hasClass(cName)) {
  4. this.removeClass(cName);
  5. } else {
  6. this.addClass(cName);
  7. }
  8. return this;
  9. }
  10. });

3、属性模块

3.1 演示jQuery中的相关方法

  • 目标:复习jQuery中常用的属性操作方法
  • 内容:attr / val / html / text
  • 特点:
    • 设置:给所有匹配到的元素设置
    • 读取:只获取第一个元素的属性或内容

3.2 attr方法 -属性操作

  • 注意:setAttribute 只能用来设置默认值。要访问或修改当前值,使用 elt[name] = value 代替
  1. itcast.fn.extend({
  2. attr: function(name, value) {
  3. if(value === undefined) {
  4. return this[0].getAttribute(name);
  5. }
  6.  
  7. return this.each(function() {
  8. // this.setAttribute(name, value);
  9. this[name] = value;
  10. });
  11. }
  12. });
  • 案例:随机切换图片

3.3 表单值操作

  1. itcast.fn.extend({
  2. val: function(value) {
  3. if(value === undefined) {
  4. return this[0].value;
  5. }
  6.  
  7. return this.each(function() {
  8. this.value = value;
  9. });
  10. }
  11. });

3.4 html 和 text操作

3.4.1 html 操作

  1. itcast.fn.extend({
  2. html: function(html) {
  3. if(html === undefined) {
  4. return this[0].innerHTML;
  5. }
  6.  
  7. return this.each(function() {
  8. this.innerHTML = html;
  9. });
  10. }
  11. });

3.4.2 text 操作

  • 注意:innerText是非标准属性
  1. itcast.extend({
  2. getInnerText: function(dom) {
  3. var textArr;
  4. if(dom.innerText !== undefined) {
  5. return dom.innerText;
  6. }
  7. textArr = getNodeText(dom);
  8. return textArr.join("");
  9.  
  10. function getNodeText(node) {
  11. var cNodes = node.childNodes,
  12. len = cNodes.length, i, cNode,
  13. arr = [];
  14.  
  15. for(i = 0; i < len; i++) {
  16. cNode = cNodes[i];
  17. if(cNode.nodeType === 3) {
  18. arr.push(cNode.nodeValue);
  19. } else if(cNodes.nodeType === 1) {
  20. arr = arr.concat( getNodeText(cNode) );
  21. }
  22. }
  23. return arr;
  24. }
  25. },
  26. setInnerText: function(dom, str) {
  27. if("innerText" in dom) {
  28. dom.innerText = str;
  29. } else {
  30. dom.innerHTML = "";
  31. dom.appendChild( document.createTextNode(str) );
  32. }
  33. }
  34. });
  35.  
  36. itcast.fn.extend({
  37. text: function(text) {
  38. if(text === undefined) {
  39. return itcast.getInnerText(this[0]);
  40. }
  41.  
  42. return this.each(function() {
  43. itcast.setInnerText(this, text);
  44. });
  45. }
  46. });

jQuery中样式和属性模块简单分析的更多相关文章

  1. jquery中attr和prop的区别分析

    这篇文章主要介绍了jquery中attr和prop的区别分析的相关资料,需要的朋友可以参考下 在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别 ...

  2. rocketmq中的NettyRemotingClient类的简单分析

    rocketmq中的NettyRemotingClient类的简单分析 Bootstrap handler = this.bootstrap.group(this.eventLoopGroupWork ...

  3. java 中 “文件” 和 “流” 的简单分析

    java 中 FIle 和 流的简单分析 File类 简单File 常用方法 创建一个File 对象,检验文件是否存在,若不存在就创建,然后对File的类的这部分操作进行演示,如文件的名称.大小等 / ...

  4. DELPHI中完成端口(IOCP)的简单分析(4)

    DELPHI中完成端口(IOCP)的简单分析(4)   在我以前写的文章中,一直说的是如何接收数据.但是对于如何发送数据却一点也没有提到.因为从代码量上来说接收的代码要比发送多很多.今天我就来写一下如 ...

  5. DELPHI中完成端口(IOCP)的简单分析(3)

    DELPHI中完成端口(IOCP)的简单分析(3)   fxh7622关注4人评论7366人阅读2007-01-17 11:18:24   最近太忙,所以没有机会来写IOCP的后续文章.今天好不容易有 ...

  6. DELPHI中完成端口(IOCP)的简单分析(2)

    DELPHI中完成端口(IOCP)的简单分析(2)   今天我写一下关于DELPHI编写完成端口(IOCP)的工作者线程中的东西.希望各位能提出批评意见.上次我写了关于常见IOCP的代码,对于IOCP ...

  7. DELPHI中完成端口(IOCP)的简单分析(1)

    DELPHI中完成端口(IOCP)的简单分析(1)   用DELPHI开发网络代码已经有一段时间了! 我发现在网上用VC来实现完成端口(IOCP)的代码很多,但是使用DELPHI来实现的就比较少了.对 ...

  8. jQuery - 02. 样式表属性操作/类操作、动画、显示隐藏、滑入、淡入、停止动画、节点操作、添加对象、清空节点

    样式表属性操作.css $("div").css({'width':100,'height':100,'background':'red'}); $("div" ...

  9. JQuery中常用的 属性选择器

    jQuery中使用$()作为选择符极大提高工作效率以及方便快捷;一些常用属性的选择器由以下几种 1) $('#id') id选择器 2) $('.class') css选择器,class类名 3) $ ...

随机推荐

  1. 学习网址Collect

    Laravel 学院    https://laravelacademy.org/wx小程序 https://developers.weixin.qq.com/miniprogram/dev/quic ...

  2. SSM项目中表单分页操作(PageHepler使用)

    Maven pom.xml添加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifa ...

  3. DFS-BFS深度优选遍历和广度优先遍历

    https://www.jianshu.com/p/b086986969e6 DFS--需要借助stack实现 stack.push stack.pop BFS--需要借助队列queue stack- ...

  4. php第五节课

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

  5. [CodeForces]1042D

    大意:求一个序列有几个子序列的和小于给定值,里面的数有正有负,序列长度≤200000. 列个式子,其实求的是sum[r]-sum[l-1]<T sum[r]-T<sum[l-1] 所以我们 ...

  6. 使用yum update更新文件系统时不更新内核的方法

    CentOS使用yum update更新时不升级内核 cp /etc/yum.conf    /etc/yum.confbak 方法一.修改yum的配置文件 vi /etc/yum.conf  在[m ...

  7. composer 安装教程

    https://getcomposer.org/download/ 邓士鹏 1.先检查php.ini是否开启ssl ;extension=php_openssl.dll   2. php -r &qu ...

  8. 读取com口接收byte数据的处理

    procedure Tfrm_CheckCloth.cnrs232ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); ...

  9. poj 1466 最大独立集

    #include<stdio.h> #include<string.h>//这个分开后男的站在一边女的站在一边,不肯能有les或者gay.最大独立集=n-最大匹配数 #defi ...

  10. Solr Update插件自定义Update Chain按条件更新索引

    背景:基于call客,来电和跟进记录等多个数据来源的用户文档,需要在更新是判断首来源的时间. 如对电话号码11xxxx来说,来电时间是今天,call客时间是昨天,而call客数据又可能因为网络原因晚上 ...