1 line
1.9 KiB
JSON
1 line
1.9 KiB
JSON
{"name":"hasClass","type":"method","title":".hasClass()","deprecated":null,"removed":null,"desc":"Determine whether any of the matched elements are assigned the given class.","categories":["attributes","manipulation/class-attribute","css","version/1.2"],"entries":[{"return":"Boolean","signatures":{"added":"1.2","argument":{"desc":"The class name to search for.","name":"className","type":"String"}},"examples":{"desc":"Looks for the paragraph that contains 'selected' as a class.","code":"\n$( \"#result1\" ).append( $( \"p:first\" ).hasClass( \"selected\" ).toString() );\n$( \"#result2\" ).append( $( \"p:last\" ).hasClass( \"selected\" ).toString() );\n$( \"#result3\" ).append( $( \"p\" ).hasClass( \"selected\" ).toString() ) ;\n","css":"\n p {\n margin: 8px;\n font-size: 16px;\n }\n .selected {\n color: red;\n }\n","html":"\n<p>This paragraph is black and is the first paragraph.</p>\n<p class=\"selected\">This paragraph is red and is the second paragraph.</p>\n<div id=\"result1\">First paragraph has selected class: </div>\n<div id=\"result2\">Second paragraph has selected class: </div>\n<div id=\"result3\">At least one paragraph has selected class: </div>\n"},"longdesc":"\n <p>Elements may have more than one class assigned to them. In HTML, this is represented by separating the class names with a space:</p>\n <pre><code>\n<div id=\"mydiv\" class=\"foo bar\"></div>\n </code></pre>\n <p>The <code>.hasClass()</code> method will return <code>true</code> if the class is assigned to an element, even if other classes also are. For example, given the HTML above, the following will return <code>true</code>:</p>\n <pre><code>\n$( \"#mydiv\" ).hasClass( \"foo\" )\n </code></pre>\n <p>As would:</p>\n <pre><code>\n$( \"#mydiv\" ).hasClass( \"bar\" )\n </code></pre>\n <p>While this would return <code>false</code>:</p>\n <pre><code>\n$( \"#mydiv\" ).hasClass( \"quux\" )\n </code></pre>\n "}]} |