mirror of
https://github.com/Hopiu/jquery-mobile.git
synced 2026-03-17 14:30:28 +00:00
139 lines
No EOL
6.4 KiB
HTML
139 lines
No EOL
6.4 KiB
HTML
<!DOCTYPE html>
|
|
<html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
|
|
<div data-role="page">
|
|
<div data-role="header">
|
|
<h1>.toggleClass()</h1>
|
|
|
|
</div>
|
|
<div data-role="content" class=" ui-body ui-body-c" id="toggleClass1">
|
|
<h2 class="jq-clearfix roundTop section-title">
|
|
<span class="name">.toggleClass( className )</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#jQuery">jQuery</a></span>
|
|
</h2>
|
|
<div class=" entry-details">
|
|
<p class="desc"><strong>Description: </strong>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.</p>
|
|
<ul class="signatures">
|
|
<li class="signature" id="toggleClass-className">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.toggleClass( className )</h4>
|
|
<p class="arguement"><strong>className</strong>One or more class names (separated by spaces) to be toggled for each element in the matched set.</p>
|
|
</li>
|
|
<li class="signature" id="toggleClass-className-switch">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.3/">1.3</a></span>.toggleClass( className, switch )</h4>
|
|
<p class="arguement"><strong>className</strong>One or more class names (separated by spaces) to be toggled for each element in the matched set.</p>
|
|
<p class="arguement"><strong>switch</strong>A boolean value to determine whether the class should be added or removed.</p>
|
|
</li>
|
|
<li class="signature" id="toggleClass-functionindex, class-switch">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>.toggleClass( function(index, class), [ switch ] )</h4>
|
|
<p class="arguement"><strong>function(index, class)</strong>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 and the old class value as arguments.</p>
|
|
<p class="arguement"><strong>switch</strong>A boolean value to determine whether the class should be added or removed.</p>
|
|
</li>
|
|
</ul>
|
|
<div class="longdesc">
|
|
<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>
|
|
<pre><div class="tumble">Some text.</div>
|
|
</pre>
|
|
<p>The first time we apply <code>$('div.tumble').toggleClass('bounce')</code>, we get the following:</p>
|
|
<pre><div class="tumble bounce">Some text.</div>
|
|
</pre>
|
|
<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>
|
|
<pre><div class="tumble">Some text.</div></pre>
|
|
<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>
|
|
<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>
|
|
<pre>$('#foo').toggleClass(className, addOrRemove);</pre>
|
|
<p>is equivalent to:</p>
|
|
<pre>if (addOrRemove) {
|
|
$('#foo').addClass(className);
|
|
}
|
|
else {
|
|
$('#foo').removeClass(className);
|
|
}
|
|
</pre>
|
|
<p>As of jQuery 1.4, the <code>.toggleClass()</code> method allows us to indicate the class name to be toggled by passing in a function.</p>
|
|
<pre>$('div.foo').toggleClass(function() {
|
|
if ($(this).parent().is('.bar')) {
|
|
return 'happy';
|
|
} else {
|
|
return 'sad';
|
|
}
|
|
});</pre>
|
|
<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>
|
|
</div>
|
|
<h3>Examples:</h3>
|
|
<div id="entry-examples" class="entry-examples">
|
|
<div id="example-0">
|
|
<h4>Example: <span class="desc">Toggle the class 'highlight' when a paragraph is clicked.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
|
|
p { margin: 4px; font-size:16px; font-weight:bolder;
|
|
cursor:pointer; }
|
|
.blue { color:blue; }
|
|
.highlight { background:yellow; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p class="blue">Click to toggle</p>
|
|
<p class="blue highlight">highlight</p>
|
|
<p class="blue">on these</p>
|
|
<p class="blue">paragraphs</p>
|
|
<script>
|
|
$("p").click(function () {
|
|
$(this).toggleClass("highlight");
|
|
});
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-1">
|
|
<h4>Example: <span class="desc">Add the "highlight" class to the clicked paragraph on every third click of that paragraph, remove it every first and second click.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
p { margin: 4px; font-size:16px; font-weight:bolder;
|
|
cursor:pointer; }
|
|
.blue { color:blue; }
|
|
.highlight { background:red; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p class="blue">Click to toggle (<span>clicks: 0</span>)</p>
|
|
<p class="blue highlight">highlight (<span>clicks: 0</span>)</p>
|
|
<p class="blue">on these (<span>clicks: 0</span>)</p>
|
|
|
|
<p class="blue">paragraphs (<span>clicks: 0</span>)</p>
|
|
<script>
|
|
var count = 0;
|
|
$("p").each(function() {
|
|
var $thisParagraph = $(this);
|
|
var count = 0;
|
|
$thisParagraph.click(function() {
|
|
count++;
|
|
$thisParagraph.find("span").text('clicks: ' + count);
|
|
$thisParagraph.toggleClass("highlight", count % 3 == 0);
|
|
});
|
|
});
|
|
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body></html> |