1 line
6.5 KiB
JSON
1 line
6.5 KiB
JSON
{"name":"toggleClass","type":"method","title":".toggleClass()","deprecated":null,"removed":null,"desc":"Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.","categories":["attributes","manipulation/class-attribute","css","version/1.0","version/1.3","version/1.4"],"entries":[{"return":"jQuery","signatures":[{"added":"1.0","argument":{"desc":"One or more class names (separated by spaces) to be toggled for each element in the matched set.","name":"className","type":"String"}},{"added":"1.3","argument":[{"desc":"One or more class names (separated by spaces) to be toggled for each element in the matched set.","name":"className","type":"String"},{"desc":"A Boolean (not just truthy/falsy) value to determine whether the class should be added or removed.","name":"switch","type":"Boolean"}]},{"added":"1.4","argument":{"desc":"A boolean value to determine whether the class should be added or removed.","name":"switch","optional":"true","type":"Boolean"}},{"added":"1.4","argument":[{"desc":"A function that returns class names to be toggled in the class attribute of each element in the matched set. Receives the index position of the element in the set, the old class value, and the switch as arguments.","name":"function(index, class, switch)","type":"Function"},{"desc":"A boolean value to determine whether the class should be added or removed.","name":"switch","optional":"true","type":"Boolean"}]}],"examples":[{"desc":"Toggle the class 'highlight' when a paragraph is clicked.","code":"\n$( \"p\" ).click(function() {\n $( this ).toggleClass( \"highlight\" );\n});\n","css":"\n p {\n margin: 4px;\n font-size: 16px;\n font-weight: bolder;\n cursor: pointer;\n }\n .blue {\n color: blue;\n }\n .highlight {\n background: yellow;\n }\n","html":"\n<p class=\"blue\">Click to toggle</p>\n<p class=\"blue highlight\">highlight</p>\n<p class=\"blue\">on these</p>\n<p class=\"blue\">paragraphs</p>\n"},{"desc":"Add the \"highlight\" class to the clicked paragraph on every third click of that paragraph, remove it every first and second click.","code":"\nvar count = 0;\n$( \"p\" ).each(function() {\n var $thisParagraph = $( this );\n var count = 0;\n $thisParagraph.click(function() {\n count++;\n $thisParagraph.find( \"span\" ).text( \"clicks: \" + count );\n $thisParagraph.toggleClass( \"highlight\", count % 3 === 0 );\n });\n});\n","css":"\n p {\n margin: 4px;\n font-size: 16px;\n font-weight: bolder;\n cursor: pointer;\n }\n .blue {\n color: blue;\n }\n .highlight {\n background: red;\n }\n","html":"\n<p class=\"blue\">Click to toggle (<span>clicks: 0</span>)</p>\n<p class=\"blue highlight\">highlight (<span>clicks: 0</span>)</p>\n<p class=\"blue\">on these (<span>clicks: 0</span>)</p>\n<p class=\"blue\">paragraphs (<span>clicks: 0</span>)</p>\n"},{"desc":"Toggle the class name(s) indicated on the buttons for each div.\n ","css":"\n .wrap > div {\n float: left;\n width: 100px;\n margin: 1em 1em 0 0;\n padding=left: 3px;\n border: 1px solid #abc;\n }\n div.a {\n background-color: aqua;\n }\n div.b {\n background-color: burlywood;\n }\n div.c {\n background-color: cornsilk;\n }\n","html":"\n<div class=\"buttons\">\n <button>toggle</button>\n <button class=\"a\">toggle a</button>\n <button class=\"a b\">toggle a b</button>\n <button class=\"a b c\">toggle a b c</button>\n <a href=\"#\">reset</a>\n</div>\n<div class=\"wrap\">\n <div></div>\n <div class=\"b\"></div>\n <div class=\"a b\"></div>\n <div class=\"a c\"></div>\n</div>\n","code":"\nvar cls = [ \"\", \"a\", \"a b\", \"a b c\" ];\nvar divs = $( \"div.wrap\" ).children();\nvar appendClass = function() {\n divs.append(function() {\n return \"<div>\" + ( this.className || \"none\" ) + \"</div>\";\n });\n};\n\nappendClass();\n\n$( \"button\" ).on( \"click\", function() {\n var tc = this.className || undefined;\n divs.toggleClass( tc );\n appendClass();\n});\n\n$( \"a\" ).on( \"click\", function( event ) {\n event.preventDefault();\n divs.empty().each(function( i ) {\n this.className = cls[ i ];\n });\n appendClass();\n});\n"}],"longdesc":"\n <p>This method takes one or more class names as its parameter. In the first version, if an element in the matched set of elements already has the class, then it is removed; if an element does not have the class, then it is added. For example, we can apply <code>.toggleClass()</code> to a simple <code><div></code>: </p>\n <pre><code>\n<div class=\"tumble\">Some text.</div>\n </code></pre>\n <p>The first time we apply <code>$( \"div.tumble\" ).toggleClass( \"bounce\" )</code>, we get the following:</p>\n <pre><code>\n<div class=\"tumble bounce\">Some text.</div>\n </code></pre>\n <p>The second time we apply <code>$( \"div.tumble\" ).toggleClass( \"bounce\" )</code>, the <code><div></code> class is returned to the single <code>tumble</code> value:</p>\n <pre><code><div class=\"tumble\">Some text.</div></code></pre>\n <p>Applying <code>.toggleClass( \"bounce spin\" )</code> to the same <code><div></code> alternates between <code><div class=\"tumble bounce spin\"></code> and <code><div class=\"tumble\"></code>.</p>\n <p>The second version of <code>.toggleClass()</code> uses the second parameter for determining whether the class should be added or removed. If this parameter's value is <code>true</code>, then the class is added; if <code>false</code>, the class is removed. In essence, the statement:</p>\n <pre><code>\n$( \"#foo\" ).toggleClass( className, addOrRemove );\n </code></pre>\n <p>is equivalent to:</p>\n <pre><code>\nif ( addOrRemove ) {\n $( \"#foo\" ).addClass( className );\n} else {\n $( \"#foo\" ).removeClass( className );\n}\n </code></pre>\n <p><strong>As of jQuery 1.4</strong>, if no arguments are passed to <code>.toggleClass()</code>, all class names on the element the first time <code>.toggleClass()</code> is called will be toggled. Also as of jQuery 1.4, the class name to be toggled can be determined by passing in a function.</p>\n <pre><code>\n$( \"div.foo\" ).toggleClass(function() {\n if ( $( this ).parent().is( \".bar\" ) ) {\n return \"happy\";\n } else {\n return \"sad\";\n }\n});\n</code></pre>\n <p>This example will toggle the <code>happy</code> class for <code><div class=\"foo\"></code> elements if their parent element has a class of <code>bar</code>; otherwise, it will toggle the <code>sad</code> class.</p>\n "}]} |