control-freak-ide/Control-Freak-Documentation/jQuery/docs/entries/eq.json
plastic-hub-dev-node-saturn 538369cff7 latest
2021-05-12 18:35:18 +02:00

1 line
2.6 KiB
JSON

{"name":"eq","type":"method","title":".eq()","deprecated":null,"removed":null,"desc":"Reduce the set of matched elements to the one at the specified index.","categories":["traversing/filtering","version/1.1.2"],"entries":[{"return":"jQuery","signatures":[{"added":"1.1.2","argument":{"desc":"An integer indicating the 0-based position of the element. ","name":"index","type":"Integer"}},{"added":"1.4","argument":{"desc":"An integer indicating the position of the element, counting backwards from the last element in the set. ","name":"-index","type":"Integer"}}],"examples":{"desc":"Turn the div with index 2 blue by adding an appropriate class.","code":"\n$( \"body\" ).find( \"div\" ).eq( 2 ).addClass( \"blue\" );\n","css":"\n div {\n width: 60px;\n height: 60px;\n margin: 10px;\n float: left;\n border: 2px solid blue;\n }\n .blue {\n background: blue;\n }\n","html":"\n<div></div>\n<div></div>\n<div></div>\n<div></div>\n<div></div>\n<div></div>\n"},"longdesc":"\n <p>Given a jQuery object that represents a set of DOM elements, the <code>.eq()</code> method constructs a new jQuery object from one element within that set. The supplied index identifies the position of this element in the set. </p>\n <p>Consider a page with a simple list on it:</p>\n <pre><code>\n&lt;ul&gt;\n &lt;li&gt;list item 1&lt;/li&gt;\n &lt;li&gt;list item 2&lt;/li&gt;\n &lt;li&gt;list item 3&lt;/li&gt;\n &lt;li&gt;list item 4&lt;/li&gt;\n &lt;li&gt;list item 5&lt;/li&gt;\n&lt;/ul&gt;\n </code></pre>\n <p>We can apply this method to the set of list items:</p>\n <pre><code>\n$( \"li\" ).eq( 2 ).css( \"background-color\", \"red\" );\n </code></pre>\n <p>The result of this call is a red background for item 3. Note that the supplied index is zero-based, and refers to the position of the element within the jQuery object, not within the DOM tree.</p>\n <p>Providing a negative number indicates a position starting from the end of the set, rather than the beginning. For example:</p>\n <pre><code>\n$( \"li\" ).eq( -2 ).css( \"background-color\", \"red\" );\n </code></pre>\n <p>This time list item 4 is turned red, since it is two from the end of the set.</p>\n <p>If an element cannot be found at the specified zero-based index, the method constructs a new jQuery object with an empty set and a <code>length</code> property of 0. </p>\n <pre><code>\n$( \"li\" ).eq( 5 ).css( \"background-color\", \"red\" );\n </code></pre>\n <p>Here, none of the list items is turned red, since <code>.eq( 5 )</code> indicates the sixth of five list items.</p>\n "}]}