📄 jquery.js
字号:
* [[DOM/Attributes#attr.28_key.2C_value_.29|above]], * a function is provided that computes the value. * * @example $("img").attr("title", function() { return this.src }); * @before <img src="test.jpg" /> * @result <img src="test.jpg" title="test.jpg" /> * @desc Sets title attribute from src attribute. * * @example $("img").attr("title", function(index) { return this.title + (i + 1); }); * @before <img title="pic" /><img title="pic" /><img title="pic" /> * @result <img title="pic1" /><img title="pic2" /><img title="pic3" /> * @desc Enumerate title attribute. * * @name attr * @type jQuery * @param String key The name of the property to set. * @param Function value A function returning the value to set. * Scope: Current element, argument: Index of current element * @cat DOM/Attributes */ attr: function( key, value, type ) { var obj = key; // Look for the case where we're accessing a style value if ( key.constructor == String ) if ( value == undefined ) return this.length && jQuery[ type || "attr" ]( this[0], key ) || undefined; else { obj = {}; obj[ key ] = value; } // Check to see if we're setting style values return this.each(function(index){ // Set all the styles for ( var prop in obj ) jQuery.attr( type ? this.style : this, prop, jQuery.prop(this, obj[prop], type, index, prop) ); }); }, /** * Access a style property on the first matched element. * This method makes it easy to retrieve a style property value * from the first matched element. * * @example $("p").css("color"); * @before <p style="color:red;">Test Paragraph.</p> * @result "red" * @desc Retrieves the color style of the first paragraph * * @example $("p").css("font-weight"); * @before <p style="font-weight: bold;">Test Paragraph.</p> * @result "bold" * @desc Retrieves the font-weight style of the first paragraph. * * @name css * @type String * @param String name The name of the property to access. * @cat CSS */ /** * Set a key/value object as style properties to all matched elements. * * This serves as the best way to set a large number of style properties * on all matched elements. * * @example $("p").css({ color: "red", background: "blue" }); * @before <p>Test Paragraph.</p> * @result <p style="color:red; background:blue;">Test Paragraph.</p> * @desc Sets color and background styles to all p elements. * * @name css * @type jQuery * @param Map properties Key/value pairs to set as style properties. * @cat CSS */ /** * Set a single style property to a value, on all matched elements. * If a number is provided, it is automatically converted into a pixel value. * * @example $("p").css("color","red"); * @before <p>Test Paragraph.</p> * @result <p style="color:red;">Test Paragraph.</p> * @desc Changes the color of all paragraphs to red * * @example $("p").css("left",30); * @before <p>Test Paragraph.</p> * @result <p style="left:30px;">Test Paragraph.</p> * @desc Changes the left of all paragraphs to "30px" * * @name css * @type jQuery * @param String key The name of the property to set. * @param String|Number value The value to set the property to. * @cat CSS */ css: function( key, value ) { return this.attr( key, value, "curCSS" ); }, /** * Get the text contents of all matched elements. The result is * a string that contains the combined text contents of all matched * elements. This method works on both HTML and XML documents. * * @example $("p").text(); * @before <p><b>Test</b> Paragraph.</p><p>Paraparagraph</p> * @result Test Paragraph.Paraparagraph * @desc Gets the concatenated text of all paragraphs * * @name text * @type String * @cat DOM/Attributes */ /** * Set the text contents of all matched elements. * * Similar to html(), but escapes HTML (replace "<" and ">" with their * HTML entities). * * @example $("p").text("<b>Some</b> new text."); * @before <p>Test Paragraph.</p> * @result <p><b>Some</b> new text.</p> * @desc Sets the text of all paragraphs. * * @example $("p").text("<b>Some</b> new text.", true); * @before <p>Test Paragraph.</p> * @result <p>Some new text.</p> * @desc Sets the text of all paragraphs. * * @name text * @type String * @param String val The text value to set the contents of the element to. * @cat DOM/Attributes */ text: function(e) { if ( typeof e == "string" ) return this.empty().append( document.createTextNode( e ) ); var t = ""; jQuery.each( e || this, function(){ jQuery.each( this.childNodes, function(){ if ( this.nodeType != 8 ) t += this.nodeType != 1 ? this.nodeValue : jQuery.fn.text([ this ]); }); }); return t; }, /** * Wrap all matched elements with a structure of other elements. * This wrapping process is most useful for injecting additional * stucture into a document, without ruining the original semantic * qualities of a document. * * This works by going through the first element * provided (which is generated, on the fly, from the provided HTML) * and finds the deepest ancestor element within its * structure - it is that element that will en-wrap everything else. * * This does not work with elements that contain text. Any necessary text * must be added after the wrapping is done. * * @example $("p").wrap("<div class='wrap'></div>"); * @before <p>Test Paragraph.</p> * @result <div class='wrap'><p>Test Paragraph.</p></div> * * @name wrap * @type jQuery * @param String html A string of HTML, that will be created on the fly and wrapped around the target. * @cat DOM/Manipulation */ /** * Wrap all matched elements with a structure of other elements. * This wrapping process is most useful for injecting additional * stucture into a document, without ruining the original semantic * qualities of a document. * * This works by going through the first element * provided and finding the deepest ancestor element within its * structure - it is that element that will en-wrap everything else. * * This does not work with elements that contain text. Any necessary text * must be added after the wrapping is done. * * @example $("p").wrap( document.getElementById('content') ); * @before <p>Test Paragraph.</p><div id="content"></div> * @result <div id="content"><p>Test Paragraph.</p></div> * * @name wrap * @type jQuery * @param Element elem A DOM element that will be wrapped around the target. * @cat DOM/Manipulation */ wrap: function() { // The elements to wrap the target around var a, args = arguments; // Wrap each of the matched elements individually return this.each(function(){ if ( !a ) a = jQuery.clean(args, this.ownerDocument); // Clone the structure that we're using to wrap var b = a[0].cloneNode(true); // Insert it before the element to be wrapped this.parentNode.insertBefore( b, this ); // Find the deepest point in the wrap structure while ( b.firstChild ) b = b.firstChild; // Move the matched element to within the wrap structure b.appendChild( this ); }); }, /** * Append content to the inside of every matched element. * * This operation is similar to doing an appendChild to all the * specified elements, adding them into the document. * * @example $("p").append("<b>Hello</b>"); * @before <p>I would like to say: </p> * @result <p>I would like to say: <b>Hello</b></p> * @desc Appends some HTML to all paragraphs. * * @example $("p").append( $("#foo")[0] ); * @before <p>I would like to say: </p><b id="foo">Hello</b> * @result <p>I would like to say: <b id="foo">Hello</b></p> * @desc Appends an Element to all paragraphs. * * @example $("p").append( $("b") ); * @before <p>I would like to say: </p><b>Hello</b> * @result <p>I would like to say: <b>Hello</b></p> * @desc Appends a jQuery object (similar to an Array of DOM Elements) to all paragraphs. * * @name append * @type jQuery * @param <Content> content Content to append to the target * @cat DOM/Manipulation * @see prepend(<Content>) * @see before(<Content>) * @see after(<Content>) */ append: function() { return this.domManip(arguments, true, 1, function(a){ this.appendChild( a ); }); }, /** * Prepend content to the inside of every matched element. * * This operation is the best way to insert elements * inside, at the beginning, of all matched elements. * * @example $("p").prepend("<b>Hello</b>"); * @before <p>I would like to say: </p> * @result <p><b>Hello</b>I would like to say: </p> * @desc Prepends some HTML to all paragraphs. * * @example $("p").prepend( $("#foo")[0] ); * @before <p>I would like to say: </p><b id="foo">Hello</b> * @result <p><b id="foo">Hello</b>I would like to say: </p> * @desc Prepends an Element to all paragraphs. * * @example $("p").prepend( $("b") ); * @before <p>I would like to say: </p><b>Hello</b> * @result <p><b>Hello</b>I would like to say: </p> * @desc Prepends a jQuery object (similar to an Array of DOM Elements) to all paragraphs. * * @name prepend * @type jQuery * @param <Content> content Content to prepend to the target. * @cat DOM/Manipulation * @see append(<Content>) * @see before(<Content>) * @see after(<Content>) */ prepend: function() { return this.domManip(arguments, true, -1, function(a){ this.insertBefore( a, this.firstChild ); }); }, /** * Insert content before each of the matched elements. * * @example $("p").before("<b>Hello</b>"); * @before <p>I would like to say: </p> * @result <b>Hello</b><p>I would like to say: </p> * @desc Inserts some HTML before all paragraphs. * * @example $("p").before( $("#foo")[0] ); * @before <p>I would like to say: </p><b id="foo">Hello</b> * @result <b id="foo">Hello</b><p>I would like to say: </p> * @desc Inserts an Element before all paragraphs. * * @example $("p").before( $("b") ); * @before <p>I would like to say: </p><b>Hello</b> * @result <b>Hello</b><p>I would like to say: </p> * @desc Inserts a jQuery object (similar to an Array of DOM Elements) before all paragraphs. * * @name before * @type jQuery * @param <Content> content Content to insert before each target. * @cat DOM/Manipulation * @see append(<Content>) * @see prepend(<Content>) * @see after(<Content>) */ before: function() { return this.domManip(arguments, false, 1, function(a){ this.parentNode.insertBefore( a, this ); }); }, /** * Insert content after each of the matched elements. * * @example $("p").after("<b>Hello</b>"); * @before <p>I would like to say: </p> * @result <p>I would like to say: </p><b>Hello</b> * @desc Inserts some HTML after all paragraphs. * * @example $("p").after( $("#foo")[0] ); * @before <b id="foo">Hello</b><p>I would like to say: </p> * @result <p>I would like to say: </p><b id="foo">Hello</b> * @desc Inserts an Element after all paragraphs. * * @example $("p").after( $("b") ); * @before <b>Hello</b><p>I would like to say: </p> * @result <p>I would like to say: </p><b>Hello</b> * @desc Inserts a jQuery object (similar to an Array of DOM Elements) after all paragraphs. * * @name after * @type jQuery * @param <Content> content Content to insert after each target. * @cat DOM/Manipulation * @see append(<Content>) * @see prepend(<Content>) * @see before(<Content>) */ after: function() { return this.domManip(arguments, false, -1, function(a){ this.parentNode.insertBefore( a, this.nextSibling ); }); }, /** * Revert the most recent 'destructive' operation, changing the set of matched elements * to its previous state (right before the destructive operation). * * If there was no destructive operation before, an empty set is returned. * * A 'destructive' operation is any operation that changes the set of * matched jQuery elements. These functions are: <code>add</code>, * <code>children</code>, <code>clone</code>, <code>filter</code>, * <code>find</code>, <code>not</code>, <code>next</code>, * <code>parent</code>, <code>parents</code>, <code>prev</code> and <code>siblings</code>. * * @example $("p").find("span").end(); * @before <p><span>Hello</span>, how are you?</p> * @result [ <p>...</p> ] * @desc Selects all paragraphs, finds span elements inside these, and reverts the * selection back to the paragraphs. * * @name end * @type jQuery * @cat DOM/Traversing */ end: function() { return this.prevObject || jQuery([]); }, /** * Searches for all elements that match the specified expression. * This method is a good way to find additional descendant * elements with which to process. * * All searching is done using a jQuery expression. The expression can be * written using CSS 1-3 Selector syntax, or basic XPath. * * @example $("p").find("span"); * @before <p><span>Hello</span>, how are you?</p> * @result [ <span>Hello</span> ] * @desc Starts with all paragraphs and searches for descendant span * elements, same as $("p span") * * @name find * @type jQuery * @param String expr An expression to search with. * @cat DOM/Traversing */ find: function(t) { return this.pushStack( jQuery.unique( jQuery.map( this, function(a){ return jQuery.find(t,a); }) ), t ); }, /** * Clone matched DOM Elements and select the clones. * * This is useful for moving copies of the elements to another * location in the DOM. * * @example $("b").clone().prependTo("p"); * @before <b>Hello</b><p>, how are you?</p> * @result <b>Hello</b><p><b>Hello</b>, how are you?</p> * @desc Clones all b elements (and selects the clones) and prepends them to all paragraphs. * * @name clone * @type jQuery * @param Boolean deep (Optional) Set to false if you don't want to clone all descendant nodes, in addition to the element itself. * @cat DOM/Manipulation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -