1 line
2.0 KiB
JSON
1 line
2.0 KiB
JSON
{"name":"has","type":"method","title":".has()","deprecated":null,"removed":null,"desc":"Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.","categories":["traversing/filtering","version/1.4"],"entries":[{"return":"jQuery","signatures":[{"added":"1.4","argument":{"desc":"A string containing a selector expression to match elements against.","name":"selector","type":"String"}},{"added":"1.4","argument":{"desc":"A DOM element to match elements against.","name":"contained","type":"Element"}}],"examples":{"desc":"Check if an element is inside another.","code":"\n$( \"ul\" ).append( \"<li>\" +\n ( $( \"ul\" ).has( \"li\" ).length ? \"Yes\" : \"No\" ) +\n \"</li>\" );\n$( \"ul\" ).has( \"li\" ).addClass( \"full\" );\n","css":"\n .full {\n border: 1px solid red;\n }\n","html":"\n<ul><li>Does the UL contain an LI?</li></ul>\n"},"longdesc":"\n <p>Given a jQuery object that represents a set of DOM elements, the <code>.has()</code> method constructs a new jQuery object from a subset of the matching elements. The supplied selector is tested against the descendants of the matching elements; the element will be included in the result if any of its descendant elements matches the selector.</p>\n <p>Consider a page with a nested list as follows:</p>\n <pre><code>\n <ul>\n <li>list item 1</li>\n <li>list item 2\n <ul>\n <li>list item 2-a</li>\n <li>list item 2-b</li>\n </ul>\n </li>\n <li>list item 3</li>\n <li>list item 4</li>\n</ul>\n </code></pre>\n <p>We can apply this method to the set of list items as follows:</p>\n <pre><code>\n$( \"li\" ).has( \"ul\" ).css( \"background-color\", \"red\" );\n </code></pre>\n <p>The result of this call is a red background for item 2, as it is the only <code><li></code> that has a <code><ul></code> among its descendants.</p>\n "}]} |