您的位置:首页 > Web前端 > JQuery

jQuery 1.6 重要变更 - 性能提升带来破坏性变更

2012-01-17 14:27 330 查看
jQuery 1.6 新版本发布已经 6天了。

其中的部分重要改进,这些包含了一些破坏性的变更:
attr()、val()和data()方法几乎没有改变,这次jQuery 1.6 优化了这类函数的性能。
我记得上次测试数据:在jQuery 1.4.2版本中attr() 函数需要的时间为 4ms ;

·jQuery 1.6 在attr()方法中支持Boolean属性;
·添加了钩子函数,允许对attr()和val()方法进行扩展;
·map()方法添加了对对象的支持(将JavaScript对象的属性映射到函数)
·添加了deferred.always(), deferred.pipe()方法,减少了代码量,提高了代码易读性;
·同步动画 – 现在所有动画都同步到相同的时间间隔. 使用新的浏览器功能,动画也更顺畅;
·find(),closest()和is()可接收所有的DOM元素和JQuery对象作为参数。

当然,所有这些新特性连同整个bug修复列表在官方博文中都有详细说明。
还有,jQuery 1.6 开发团队对破坏性更新也作了详细说明。

[align=left]变更[/align]
[align=left]JQuery 1.5.2 示例[/align]
[align=left]JQuery 1.6 示例[/align]
[align=left]data()方法使用破折号创建驼峰格式。[/align]
[align=left]data-max-value="15" 创建{ max-value: 15 }[/align]
[align=left]data-max-value="15" 创建{ maxValue: 15 }[/align]
[align=left]区分了Property与Attribute。attr()方法现在不能用于获取property的值。为了处理property的值,引入了prop()和removeProp()方法。[/align]
如果复选框被选中,checkbox.attr("checked")返回(true)

很显然,第二项变更将会影响到现有打算升级到1.6的那些项目,悲剧啊。

查看 jQuery 1.6 更新官网博客【英文原版】
查看 jQuery 1.6 更新官网博客【中文版股沟翻译版本】:给英语没过四级的人看,现在知道:没文化、真可怕了吧?)

部分原文:

jQuery 1.6 Change Log

All of the API changes that occurred in this release can be found in the jQuery API documentation:

http://api.jquery.com/category/version/1.6/

Breaking Changes

In most releases we try to main compatibility with existing code. However, there are a few cases where jQuery 1.6 may require changes to existing code:

Case-mapping of
data-
attributes

jQuery 1.5 introduced a feature in the
.data()
method to automatically import any
data-
attributes that were set on the element and convert them to JavaScript values using JSON semantics. In jQuery 1.6 we have updated this feature to match the W3C HTML5 spec with regards to camel-casing data attributes that have embedded dashes. So for example in jQuery 1.5.2, an attribute of
data-max-value="15"
would create a data object of
{ max-value: 15 }
but as of jQuery 1.6 it sets
{ maxValue: 15 }
.

.prop()
,
.removeProp()
, and
.attr()

In the 1.6 release we’ve split apart the handling of DOM attributes and DOM properties into separate methods. The new
.prop()
method sets or gets properties on DOM elements, and
.removeProp()
removes properties. In the past, jQuery has not drawn a clear line between properties and attributes. Generally, DOM attributes represent the state of DOM information as retrieved from the document, such as the
value
attribute in the markup
<input type="text" value="abc">
. DOM properties represent the dynamic state of the document; for example if the user clicks in the input element above and types
def
the
.prop("value")
is
abcdef
but the
.attr("value")
remains
abc
.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: