File: index.html

package info (click to toggle)
jqapi 1.7%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,288 kB
  • sloc: javascript: 632; makefile: 12
file content (216 lines) | stat: -rw-r--r-- 9,550 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
<div class="entry-content">
        <div class="entry-title roundTop">
          
          <h1 class="jq-clearfix">.toggleClass()</h1>
          <div class="entry-meta jq-clearfix">
                        Categories:
            <span class="category"><a href="http://api.jquery.com/category/attributes/" title="View all posts in Attributes">Attributes</a></span> | <span class="category"><a href="http://api.jquery.com/category/manipulation/" title="View all posts in Manipulation">Manipulation</a> &gt; <a href="http://api.jquery.com/category/manipulation/class-attribute/" title="View all posts in Class Attribute">Class Attribute</a></span> | <span class="category"><a href="http://api.jquery.com/category/css/" title="View all posts in CSS">CSS</a></span>
  

          </div>

</div>
<div id="toggleClass1" class="entry method">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">.toggleClass( className )</span> <span class="returns">Returns: <a class="return" href="http://api.jquery.com/Types/#jQuery">jQuery</a></span>
</h2>
<div class="jq-box roundBottom 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 (not just truthy/falsy) value to determine whether the class should be added or removed.</p>
</li>
<li class="signature" id="toggleClass-switch">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>.toggleClass(  [switch] )</h4>
<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-switch">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>.toggleClass( function(index, class, switch) [, switch] )</h4>
<p class="arguement"><strong>function(index, class, switch)</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, the old class value, and the switch 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>&lt;div&gt;</code>: </p>
<pre>&lt;div class="tumble"&gt;Some text.&lt;/div&gt;
      </pre>
<p>The first time we apply <code>$('div.tumble').toggleClass('bounce')</code>, we get the following:</p>
<pre>&lt;div class="tumble bounce"&gt;Some text.&lt;/div&gt;
      </pre>
<p>The second time we apply <code>$('div.tumble').toggleClass('bounce')</code>, the <code>&lt;div&gt;</code> class is returned to the single <code>tumble</code> value:</p>
<pre>&lt;div class="tumble"&gt;Some text.&lt;/div&gt;</pre>
<p>Applying <code>.toggleClass('bounce spin')</code> to the same <code>&lt;div&gt;</code> alternates between <code>&lt;div class="tumble bounce spin"&gt;</code> and <code>&lt;div class="tumble"&gt;</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><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>
<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>&lt;div class="foo"&gt;</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 class="entry-examples" id="entry-examples">
<div id="example-0">
<h4>Example: <span class="desc">Toggle the class 'highlight' when a paragraph is clicked.</span>
</h4>
<pre class="prettyprint"><code class="example demo-code">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;style&gt;

  p { margin: 4px; font-size:16px; font-weight:bolder;
      cursor:pointer; }
  .blue { color:blue; }
  .highlight { background:yellow; }
  &lt;/style&gt;
  &lt;script src="http://code.jquery.com/jquery-1.7rc2.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;p class="blue"&gt;Click to toggle&lt;/p&gt;
  &lt;p class="blue highlight"&gt;highlight&lt;/p&gt;
  &lt;p class="blue"&gt;on these&lt;/p&gt;
  &lt;p class="blue"&gt;paragraphs&lt;/p&gt;
&lt;script&gt;
    $("p").click(function () {
      $(this).toggleClass("highlight");
    });
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;</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 class="prettyprint"><code class="example demo-code">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;style&gt;
  p { margin: 4px; font-size:16px; font-weight:bolder;
      cursor:pointer; }
  .blue { color:blue; }
  .highlight { background:red; }
  &lt;/style&gt;
  &lt;script src="http://code.jquery.com/jquery-1.7rc2.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;p class="blue"&gt;Click to toggle (&lt;span&gt;clicks: 0&lt;/span&gt;)&lt;/p&gt;
  &lt;p class="blue highlight"&gt;highlight (&lt;span&gt;clicks: 0&lt;/span&gt;)&lt;/p&gt;
  &lt;p class="blue"&gt;on these (&lt;span&gt;clicks: 0&lt;/span&gt;)&lt;/p&gt;

  &lt;p class="blue"&gt;paragraphs (&lt;span&gt;clicks: 0&lt;/span&gt;)&lt;/p&gt;
&lt;script&gt;
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);
  });
});

&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;</code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div id="example-2">
<h4>Example: <span class="desc">Toggle the class name(s) indicated on the buttons for each div.
  </span>
</h4>
<pre class="prettyprint"><code class="example demo-code">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;style&gt;
.wrap &gt; div { float: left; width: 100px; margin: 1em 1em 0 0;
              padding=left: 3px; border: 1px solid #abc; }
div.a { background-color: aqua; }
div.b { background-color: burlywood; }
div.c { background-color: cornsilk; }
&lt;/style&gt;
  &lt;script src="http://code.jquery.com/jquery-1.7rc2.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  
&lt;div class="buttons"&gt;
  &lt;button&gt;toggle&lt;/button&gt;
  &lt;button class="a"&gt;toggle a&lt;/button&gt;
  &lt;button class="a b"&gt;toggle a b&lt;/button&gt;
  &lt;button class="a b c"&gt;toggle a b c&lt;/button&gt;
  &lt;a href="#"&gt;reset&lt;/a&gt;
&lt;/div&gt;
&lt;div class="wrap"&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div class="b"&gt;&lt;/div&gt;
  &lt;div class="a b"&gt;&lt;/div&gt;
  &lt;div class="a c"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;script&gt;
var cls = ['', 'a', 'a b', 'a b c'];
var divs = $('div.wrap').children();
var appendClass = function() {
  divs.append(function() {
    return '&lt;div&gt;' + (this.className || 'none') + '&lt;/div&gt;';
  });
};

appendClass();

$('button').bind('click', function() {
  var tc = this.className || undefined;
  divs.toggleClass(tc);
  appendClass();
});

$('a').bind('click', function(event) {
  event.preventDefault();
  divs.empty().each(function(i) {
    this.className = cls[i];
  });
  appendClass();
});
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;</code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
</div>
</div>
</div>

        </div>

</body></html>