jQuery1.1 API 中文版 第四部分JavaScript

jQuery1.1 API 中文版 第四部分JavaScript
url: http://jquery.org.cn/visual/cn/index.xml

第四部分JavaScript


1. $.browser
包含从navigator.userAgent属性中读取的用户代理标签(代理检测)。有效的标签有:safari, opera, msie, mozilla 这个属性在DOM载入完成之前就可以访问,所以可以使用它来针对某个浏览器添加ready事件。 有的时候,对象检测可能不太可靠,这时就有必要使用浏览器检测。最好是两种方式都不用! 把浏览器和对象检测结合使用,可以得到相当可靠的结果。

Contains flags for the useragent, read from navigator.userAgent. Available flags are: safari, opera, msie, mozilla This property is available before the DOM is ready, therefore you can use it to add ready events only for certain browsers. There are situations where object detections is not reliable enough, in that cases it makes sense to use browser detection. Simply try to avoid both! A combination of browser and object detection yields quite reliable results.

返回值
Boolean

示例
说明:
如果当前的用户代理是IE的某个版本就返回true。

jQuery 代码:
$.browser.msie
 
--------------------------------------------------------------------------------

 

说明:
只在safari浏览器中提示-"this is safari!"。

jQuery 代码:
if($.browser.safari) { $( function() { alert("this is safari!"); } ); }

 

2. $.each(obj, fn)
一个通用的迭代函数,可用于近似地迭代对象和数组。这个函数与$().each()不同,$().each()是专门用于迭代和执行jQuery对象的函数。而这个函数可以用于迭代任何对象和数组。 这个函数的回调中包含两个参数:第一个是key(对象)或index(数组),第二个是值。

A generic iterator function, which can be used to seemlessly iterate over both objects and arrays. This function is not the same as $().each() - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything. The callback has two arguments:the key (objects) or index (arrays) as first the first, and the value as the second.

返回值
Object

参数
obj (Object): 要重复迭代的对象或数组
fn (Function): 要在每个对象中执行的函数
示例
说明:
这是一个迭代数组中所有项目的例子,通过函数访问了其中的每个项目和索引。

jQuery 代码:
$.each( [0,1,2], function(i, n){ alert( "Item #" + i + ": " + n ); });
 
--------------------------------------------------------------------------------

 

说明:
这是一个迭代对象中所有属性的例子,通过函数访问了每个属性的名称和值。

jQuery 代码:
$.each( { name: "John", lang: "JS" }, function(i, n){ alert( "Name: " + i + ", Value: " + n ); });

 

3. $.extend(target, prop1, propN)
用一个或多个其他对象来扩展一个对象,返回这个被扩展的对象。这是简化继承的主要工具。

Extend one object with one or more others, returning the original, modified, object. This is a great utility for simple inheritance.

返回值
Object

参数
target (Object): 要扩展的对象
prop1 (Object): 要与第一个对象合并的对象
propN (Object): (可选) 更多要与第一个对象合并的对象
示例
说明:
合并settings和options, 修改并返回settings

jQuery 代码:
var settings = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; jQuery.extend(settings, options);
 
结果:
settings == { validate: true, limit: 5, name: "bar" }
 
--------------------------------------------------------------------------------

 

说明:
合并defaults和options, 但不修改defaults,返回合并后的对象

jQuery 代码:
var defaults = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; var settings = jQuery.extend({}, defaults, options);
 
结果:
settings == { validate: true, limit: 5, name: "bar" }

 

4. $.grep(array, fn, inv)
使用筛选函数,从一个数组中筛选项目。 其中筛选函数必须传递两个参数:数组中的当前项目和数组中项目的索引。如果要保持数组中的项目,这个函数必须返回true;如果返回false,就会删除项目。

Filter items out of an array, by using a filter function. The specified function will be passed two arguments: The current array item and the index of the item in the array. The function must return 'true' to keep the item in the array, false to remove it.

返回值
Array

参数
array (Array): 要在其中查找项目的数组
fn (Function): 处理数组项目的函数
inv (Boolean): 反转选项 - 选择相反的筛选结果
示例
jQuery 代码:
$.grep( [0,1,2], function(i){ return i > 0; });
 
结果:
[1, 2]

 

5. $.map(array, fn)
把一个数组中的项目转换到另一个数组中。 作为参数的转换函数会被每个数组项目调用,而且会给这个转换函数传递一个表示要转换的项目的参数。 转换函数可以返回转换后的值、null(删除数组中的项目)或一个包含值的数组--表示对原始数组项目的扩展模式。

Translate all items in an array to another array of items. The translation function that is provided to this method is called for each item in the array and is passed one argument: The item to be translated. The function can then return the translated value, 'null' (to remove the item), or an array of values - which will be flattened into the full array.

返回值
Array

参数
array (Array): 要转换的数组
fn (Function): 处理数组项目的函数
示例
说明:
把原始的数组映射到一个新数组中,并给新数组中的每个值都加上4。

jQuery 代码:
$.map( [0,1,2], function(i){ return i + 4; });
 
结果:
[4, 5, 6]
 
--------------------------------------------------------------------------------

 

 

说明:
把原始的数组映射到一个新数组中,如果新数组中的值大于0,就给这个值加上1;否则将这值删除。

jQuery 代码:
$.map( [0,1,2], function(i){ return i > 0 ? i + 1 : null; });
 
结果:
[2, 3]
 
--------------------------------------------------------------------------------

 

 

说明:
把原始的数组映射到一个新数组中,将新数组中的每一个元素都扩展为两个,其中一个是其原始值,另一个是加上1之后的值。

jQuery 代码:
$.map( [0,1,2], function(i){ return [ i, i + 1 ]; });
 
结果:
[0, 1, 1, 2, 2, 3]

 

6. $.merge(first, second)
合并两个数组,删除其中重复的项目。 得到的新数组是:第一个数组中的所有项目,加上第二个数组中唯一的(不与第一个数组中任何项目相同的)项目。

Merge two arrays together, removing all duplicates. The new array is: All the results from the first array, followed by the unique results from the second array.

返回值
Array

参数
first (Array): 要合并的第一个数组
second (Array): 要合并的第二个数组
示例
说明:
合并两个数组,删除其中重复的2

jQuery 代码:
$.merge( [0,1,2], [2,3,4] )
 
结果:
[0,1,2,3,4]
 
--------------------------------------------------------------------------------

 

说明:
合并两个数组,删除重复的3和2。

jQuery 代码:
$.merge( [3,2,1], [4,3,2] )
 
结果:
[3,2,1,4]

 

7. $.trim(str)
删除字符串两端的空白字符。

Remove the whitespace from the beginning and end of a string.

返回值
String

参数
str (String): 要删除空白字符的字符串
示例
jQuery 代码:
$.trim(" hello, how are you? ");
 
结果:
"hello, how are you?"

 

jquery中文汉化版 (function( window, undefined ) { //不要做这个因为各自的应用程序包括ASP.NET查找 // the stack via arguments.caller.callee and Firefox dies if //你尝试查找通过“精确使用”呼叫链接(#13335) //支持:火狐浏览器 18+ //“精确使用”; var //deferred对象被使用在DOM(Document Object Model翻译:文档对象模型)准备之时 //deferred(延迟)对象:从jQuery 1.5.0版本开始引入的一个新功能 //在DOM准备好时调用 readyList, //一个中心引用对于jQuery根文档 //对根jQuery对象的主要引用 rootjQuery, //支持:IE9之前的版本 // For `typeof node.method` instead of `node.method !== undefined` core_strundefined = typeof undefined, // Use the correct document accordingly with window argument (sandbox) document = window.document,//window文档赋值给变量document location = [removed], // Map over jQuery in case of overwrite(不确定,待修正,希望高人帮忙翻译一下) //在jQuery上绘制写在上面的实例 //防止被覆盖 _jQuery = window.jQuery, // Map over the $ in case of overwrite _$ = window.$, //将window正则表达式符号$赋值给变量_$ //[类]:成双类型 class2type = {}, //在贮存区被删除数据ID的列表,我们能够再用他们 core_deletedIds = [], core_version = "1.9.1", //保存一个参考给一些核心的方法 //为核心方法创建引用 core_concat = core_deletedIds.concat, core_push = core_deletedIds.push, core_slice = core_deletedIds.slice, core_indexOf = core_deletedIds.indexOf, core_toString = class2type.toString, core_hasOwn = class2type.hasOwnProperty, core_trim = core_version.trim, //规定一个jQuery本地代码 //构建jQuery对象 jQuery = function( selector, context ) { //jQuery对象是实际上初始化名为enhanced(提高的)构造器 //jQuery对象实际上只是增强的初始化构造方法 return new jQuery.fn.init( selector, context, rootjQuery ); }, /* 用来匹配数字的正则,匹配可选正负号、浮点型、整型、科学计数法 * 没有使用(?)来表示可选而是通过(|)来选择 * (?:\d*\.|)匹配浮点数时,|前的\d*\.可以匹配整数部分和小数点,小数部分由后面的\d+匹配 * 匹配整数时,|)可以保证匹配继续向下进行,整数由后面的\d+匹配,同样的\d+在匹配整型和浮点型时负责的匹配部分不同 * [eE][\-+]?\d+|)处理科学计数法的匹配,同样没有使用?表示可选 */ core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source, //用于分开空格 core_rnotwhite = /\S+/g, //查找非空白字符串 // Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE) rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, //\uFEFF:字节顺序标志 //一个简单途径用于检查HTML字符串 // Prioritize #id over <tag> to avoid XSS via location.hash (#9521) // Strict HTML recognition (#11290: must start with <) rquickExpr = /^(?:(<[\w\W]+>)[^>]*|#([\w-]*))$/, //匹配一个独立的标签 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/, // JSON RegExp(JavaScript Object Notation:JavaScript对象标记法正则表达式) rvalidchars = /^[\],:{}\s]*$/, rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g, rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g, rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g, // Matches dashed string for camelizing rmsPrefix = /^-ms-/, rdashAlpha = /-([\da-z])/gi, //以上为正则运算表达式各种形式,不太容易理解,尽量掌握。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值