jQuery学习笔记

工具函数

jQuery对象序列

.map()
遍历所有成员

$(':checkbox').map(function() {
  return this.id;
}).get().join(',');


$(':checkbox').map(function(index, node) {
  return node.id;
}).get().join(',');

.slice()
序列切片, 支持一个或两个参数,支持负数

$('li').slice(2).css('background-color', 'red');
$('li').slice(2, 4).css('background-color', 'red');
$('li').slice(-2, -1).css('background-color', 'red');

通用工具

$.each() $.map()
遍历列表, $.map() 可以作用于对象。

$.each([52, 97], function(index, value) { 
  alert(index + ': ' + value); 
});

$.map( [0,1,2], function(index, n){
  return n + 4;
});
//[4, 5, 6]

$.map( [0,1,2], function(n){
  return n > 0 ? n + 1 : null;
});
//[2, 3]

$.map( [0,1,2], function(n){
  return [ n, n + 1 ];
});
//[0, 1, 1, 2, 2, 3]

var dimensions = { width: 10, height: 15, length: 20 };
$.map( dimensions, function( value, key ) {
  return value * 2;
});
//[20, 30, 40] 

var dimensions = { width: 10, height: 15, length: 20 },
$.map( dimensions, function( value, key ) {
  return key;
});
//["width", "height", "length"]

$.extend()
合并对象,第一个参数表示是否进行递归深入

var object = $.extend({}, object1, object2);
var object = $.extend(true, {}, object1, object2);

$.merge()
合并列表

$.merge( [0,1,2], [2,3,4] )
//[0,1,2,2,3,4] 

$.grep()
过滤列表,第三个参数表示是否为取反

$.grep( [0,1,2], function(array,index){ return n > 0; }); //[1,2]
$.grep( [0,1,2], function(array,index){ return n > 0; }, true); //[0]

$.inArray()
存在判断

$.inArray( value, array [, fromIndex] )

$.isArray() $.isEmptyObject() $.isFunction() $.isNumeric() $.isPlainObject() $.isWindow() $.isXMLDoc()
类型判断

$.noop()
空函数

$.now()
当前时间戳,值为 (new Date).getTime()

$.parseJson() $.parseXML()
把字符串解析为对象

var xml = "RSS Title",
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$title = $xml.find( "title" );

$.trim()
去头去尾, $.trim(str)
$.type()
判定参数的类型

# If the object is undefined or null,
#then "undefined" or "null" is returned accordingly.

* jQuery.type(undefined) === "undefined"
* jQuery.type() === "undefined"
* jQuery.type(window.notDefined) === "undefined"
* jQuery.type(null) === "null"

# If the object has an internal [[Class]] equivalent to
#one of the browser's built-in objects, the associated name is returned.
#(More details about this technique.)

* jQuery.type(true) === "boolean"
* jQuery.type(3) === "number"
* jQuery.type("test") === "string"
* jQuery.type(function(){}) === "function"
* jQuery.type([]) === "array"
* jQuery.type(new Date()) === "date"
* jQuery.type(/test/) === "regexp"

$.unique()
遍历后去重, $.unique(array)

上一篇: 下一篇: