jquery的css详解(二)
jq的工具方法style用于设置样式,jq的实例方法css在设置样式时就是调用的它,接下来分析一下源码。
jQuery.extend({
............................
style: function( elem, name, value, extra ) {
// Don't set styles on text and comment nodes
if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) {
return;
}
// Make sure that we're working with the right name
var ret, type, hooks,
origName = jQuery.camelCase( name ),
style = elem.style;
name = jQuery.cssProps[ origName ] || ( jQuery.cssProps[ origName ] = vendorPropName( style, origName ) );
// gets hook for the prefixed version
// followed by the unprefixed version
hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
// Check if we're setting a value
if ( value !== undefined ) {
type = typeof value;
// convert relative number strings (+= or -=) to relative numbers. #7345
if ( type === "string" && (ret = rrelNum.exec( value )) ) {
value = ( ret[1] + 1 ) * ret[2] + parseFloat( jQuery.css( elem, name ) );
// Fixes bug #9237
type = "number";
}
// Make sure that NaN and null values aren't set. See: #7116
if ( value == null || type === "number" && isNaN( value ) ) {
return;
}
// If a number was passed in, add 'px' to the (except for certain CSS properties)
if ( type === "number" && !jQuery.cssNumber[ origName ] ) {
value += "px";
}
// If a hook was provided, use that value, otherwise just set the specified value
if ( !hooks || !("set" in hooks) || (value = hooks.set( elem, value, extra )) !== undefined ) {
// Wrapped to prevent IE from throwing errors when 'invalid' values are provided
// Fixes bug #5509
try {
style[ name ] = value;
} catch(e) {}
}
} else {
// If a hook was provided get the non-computed value from there
if ( hooks && "get" in hooks && (ret = hooks.get( elem, false, extra )) !== undefined ) {
return ret;
}
// Otherwise just get the value from the style object
return style[ name ];
}
},
...........................................
});
代码开始判断了dom节点类型,不是元素节点类型的直接return掉
if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) {
return;
}
下面的代码跟jq的工具方法css一样,就不再分析了,不明白的朋友请点击 -> jquery的css详解(一)
var ret, type, hooks,
origName = jQuery.camelCase( name ),
style = elem.style; name = jQuery.cssProps[ origName ] || ( jQuery.cssProps[ origName ] = vendorPropName( style, origName ) ); // gets hook for the prefixed version
// followed by the unprefixed version
hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
然后判断value是否undefined,是的话则获取,否则设置
if ( value !== undefined ) {
.................
} else {
................
}
我们先看设置中的代码,先获取value的类型
type = typeof value;
如果type是字符串并且符合正则条件,则经过处理后赋值给value,type修改为number
这里的正则不解释了,有兴趣的朋友可以自己研究一下,我说一下这段代码在开发中的使用场景。
$(selector).css('width','+=100');
if ( type === "string" && (ret = rrelNum.exec( value )) ) {
value = ( ret[1] + 1 ) * ret[2] + parseFloat( jQuery.css( elem, name ) );
// Fixes bug #9237
type = "number";
}
对不正确的值返回
if ( value == null || type === "number" && isNaN( value ) ) {
return;
}
type是数字并且origName不是cssNumber的属性,则样式值加px
if ( type === "number" && !jQuery.cssNumber[ origName ] ) {
value += "px";
}
cssNumber: {
"fillOpacity": true,
"fontWeight": true,
"lineHeight": true,
"opacity": true,
"orphans": true,
"widows": true,
"zIndex": true,
"zoom": true
}
针对尺寸的hooks
if ( !hooks || !("set" in hooks) || (value = hooks.set( elem, value, extra )) !== undefined ) {
// Wrapped to prevent IE from throwing errors when 'invalid' values are provided
// Fixes bug #5509
try {
style[ name ] = value;
} catch(e) {}
}
好了,设置部分分析完了,获取部分很简单
先针对尺寸的hooks,然后用style对应的属性得到样式值
if ( hooks && "get" in hooks && (ret = hooks.get( elem, false, extra )) !== undefined ) {
return ret;
}
// Otherwise just get the value from the style object
return style[ name ];
jquery的css详解(二)的更多相关文章
- jquery的css详解(一)
通过阅读源码可以发现css是jq的实例方法.而在内部调用jq的工具方法access来实现的,对该方法不了解的朋友请点击 -> jquery工具方法access详解 在access的回调中做了一个 ...
- jQuery.attr() 函数详解
一,jQuery.attr() 函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...
- jQuery 事件用法详解
jQuery 事件用法详解 目录 简介 实现原理 事件操作 绑定事件 解除事件 触发事件 事件委托 事件操作进阶 阻止默认事件 阻止事件传播 阻止事件向后执行 命名空间 自定义事件 事件队列 jque ...
- DIV+CSS详解
DIV+CSS详解 ✪DIV+CSS"这种叫法其实是一种不准确的叫法 在做笔记的最前面必须先给大家纠正一个错误,就是"DIV+CSS"这种叫法其实是一种不准确的叫法,是国 ...
- jQuery.ready() 函数详解
jQuery.ready() 函数详解 ready()函数用于在当前文档结构载入完毕后立即执行指定的函数. 该函数的作用相当于window.onload事件. 你可以多次调用该函数,从而绑定多个函数, ...
- jQuery extend方法详解
先说个概念的东西: jQuery为开发插件提拱了两个方法,分别是: $.fn.extend(item):为每一个实例添加一个实例方法item.($("#btn1") 会生成一个 j ...
- 【转载】jQuery.extend 函数详解
转载自:http://www.cnblogs.com/RascallySnake/archive/2010/05/07/1729563.html jQuery.extend 函数详解 JQuery的e ...
- jQuery.hasClass() 函数详解
jQuery.hasClass() 函数详解 hasClass()函数用于指示当前jQuery对象所匹配的元素是否含有指定的css类名. 该函数属于jQuery对象(实例). 语法 JavaScrip ...
- Shiro 安全框架详解二(概念+权限案例实现)
Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...
随机推荐
- PHP工作笔记:yii2各种功能汇总
数据库操纵: 基础知识:数据库基础 查询语句: 使用yii migrate管理.生成数据库 Yii2 AR find用法 Yii2.0 对数据库 查询的一些简单的操作 修改语句: Yii 修改数据的两 ...
- Nodejs与ES6系列3:generator对象
3.generator对象 Generator函数是ES6提供的一种异步编程解决方案,语法行为与传统函数完全不同.Generator的中文翻译是生成器,它是ECMAScript6(代号harmory) ...
- less
//:此注释不会被编译 /**/:会被编译 变量:以@开头,exp:@test_width:100px; 混合: 匹配: 运算: 嵌套: arguments:将所有的变量带进去
- [deviceone开发]-利用do_ListView模拟单选功能
一.简介 这个是利用do_ListView组件实现多个选项里选择一项的功能,示例很简单,但是有助于理解复用机制,也可以直接参考使用.初学者推荐.二.效果图 三.相关下载 https://github. ...
- input checkbox 扩大点击范围
<div style="width:100%;height:100px;"><input type="checkbox" onclick=&q ...
- GIS公交查询-flex/java
开发语言是flex.java,开发平台是myeclise.eclise,开发接口是arcgis api for flex,提供以下的功能: 1.站名-站名查询: 2.站点查询: 3.路线查询: 备注: ...
- 关于ArcGIS API for JavaScript中basemap的总结介绍(一)
实际上basemap这个概念并不只在arcgis中才有,在Python中有一个matplotlib basemap toolkit(https://pypi.python.org/pypi/basem ...
- Centos6_64 rpm打包实践
安装rpmbuild sudo yum install rpm-build 普通用户的配置 /home/you/rpmbuild/ 假设是要构建rpm的目录 先说说rpmbuild有什么用,给一个例子 ...
- Android开发学习——SQLite数据库与单元测试
SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHelper public class Myopenhelper extends SQLiteOpenHelp ...
- EditText 显示明文和密码
1.效果图 2.布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xml ...