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?"