默认情况下, 如果没有指定context参数,$()将在当前的 HTML document中查找 DOM 元素;如果指定了 context 参数,如一个 DOM 元素集或 jQuery 对象,那就会在这个 context 中查找。在jQuery 1.3.2以后,其返回的元素顺序等同于在context中出现的先后顺序。
选择器 部分获取更多用于 expression 参数的 CSS 语法的信息。
参数
selector,[context] String,Element,/jQuery
selector:用来查找的字符串
context:作为待查找的 DOM 元素集、文档或 jQuery 对象。
element Element
一个用于封装成jQuery对象的DOM元素
object object
一个用于封装成jQuery对象
elementArray Element
一个用于封装成jQuery对象的DOM元素数组。
jQuery object object
一个用于克隆的jQuery对象。
jQuery()
返回一个空的jQuery对象。
找到所有 p 元素,并且这些元素都必须是 div 元素的子元素。
$("div > p");
设置页面背景色。
$(document.body).css( "background", "black" );
隐藏一个表单中所有元素。
$(myForm.elements).hide()
在文档的第一个表单中,查找所有的单选按钮(即: type 值为 radio 的 input 元素)。
$("input:radio", document.forms[0]);
在一个由 AJAX 返回的 XML 文档中,查找所有的 div 元素。
$("div", xml.responseXML);
jQuery(html,[ownerDocument])
概述
根据提供的原始 HTML 标记字符串,动态创建由 jQuery 对象包装的 DOM 元素。同时设置一系列的属性、事件等。
if (typeof module === "object" && typeof module.exports === "object") { // For CommonJS and CommonJS-like environments where a proper window is present, // execute the factory and get jQuery // For environments that do not inherently posses a window with a document // (such as Node.js), expose a jQuery-making factory as module.exports // This accentuates the need for the creation of a real window // e.g. var jQuery = require("jquery")(window); // See ticket #14549 for more info module.exports = global.document ? factory(global, true) : function (w) { if (!w.document) { throw new Error("jQuery requires a window with a document"); } return factory(w); }; } else { factory(global); }
// Pass this if window is not defined yet }(typeof window !== "undefined" ? window : this, function (window, noGlobal) {
// Can't do this because several apps including ASP.NET trace // the stack via arguments.caller.callee and Firefox dies if // you try to trace through "use strict" call chains. (#13335) // Support: Firefox 18+ //
var deletedIds = [];
var slice = deletedIds.slice;
var concat = deletedIds.concat;
var push = deletedIds.push;
var indexOf = deletedIds.indexOf;
var class2type = {};
var toString = class2type.toString;
var hasOwn = class2type.hasOwnProperty;
var support = {};
var version = "1.11.3",
// Define a local copy of jQuery jQuery = function (selector, context) { // The jQuery object is actually just the init constructor 'enhanced' // Need init if jQuery is called (just allow error to be thrown if not included) return new jQuery.fn.init(selector, context); }
jQuery.fn = jQuery.prototype = { // The current version of jQuery being used jquery: version,
constructor: jQuery,
// Start with an empty selector selector: "",
// The default length of a jQuery object is 0 length: 0, // For internal use only. // Behaves like an Array's method, not like a jQuery method. push: push, sort: deletedIds.sort, splice: deletedIds.splice };
jQuery.extend = jQuery.fn.extend = function () { var src, copyIsArray, copy, name, options, clone, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false;
// Handle a deep copy situation if (typeof target === "boolean") { deep = target;
// skip the boolean and the target target = arguments[i] || {}; i++; }
// Handle case when target is a string or something (possible in deep copy) if (typeof target !== "object" && !jQuery.isFunction(target)) { target = {}; }
// extend jQuery itself if only one argument is passed if (i === length) { target = this; i--; }
for (; i // Only deal with non-null/undefined values if ((options = arguments[i]) != null) { // Extend the base object for (name in options) { src = target[name]; copy = options[name];
// Never move original objects, clone them target[name] = jQuery.extend(deep, clone, copy);
// Don't bring in undefined values } else if (copy !== undefined) { target[name] = copy; } } } }
// Return the modified object return target; };
// Populate the class2type map jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function (i, name) { class2type["[object " + name + "]"] = name.toLowerCase(); });
// The number of elements contained in the matched element set jQuery.fn.size = function () { return this.length; };