Heim >Web-Frontend >js-Tutorial >Detaillierte Erläuterung der Verwendung der jQuery.queue()-Instanz
Die Funktion
Diese Funktion gehört zum jQuery
-Objekt (Instanz). Wenn Sie die erste Funktion in der Warteschlange entfernen und ausführen müssen, verwenden Sie die Funktion dequeue(). Sie können auch die Funktion „clearQueue()“ verwenden, um die angegebene Warteschlange zu löschen.
jQuery 1.2 Diese Funktion wurde hinzugefügt. Die Funktion queue()
hat die folgenden zwei Verwendungszwecke:
Verwendung 1:
jQueryObject.queue( [ queueName ] [, newQueue ] )
Wenn keine Parameter angegeben sind oder nur der Parameter queueName
angegeben ist, es bedeutet Get die Funktionswarteschlange mit dem angegebenen Namen. Wenn der Parameter newQueue
angegeben ist, bedeutet dies, dass die neue Warteschlange newQueue
verwendet wird, um alle Inhalte in der aktuellen Warteschlange festzulegen (zu ersetzen).
Verwendung 2:
jQueryObject.queue( [ queueName ,] callback )
Fügen Sie die angegebene Funktion zur angegebenen Warteschlange hinzu (Ende).
Hinweis: Alle Einstellungsvorgänge der Funktion queue()
gelten für jedes -Element, das mit dem aktuellen jQuery-Objekt übereinstimmt; alle Lesevorgänge gelten nur für das erste passende Elemente.
Parameternamen.
Parameter | Beschreibung | ||||||||
---|---|---|---|---|---|---|---|---|---|
queueName | Optional/String-Typ
|
||||||||
newQueue |
Optional/Array-TypEine neue Warteschlange, die den Inhalt der aktuellen Warteschlange ersetzt. |
||||||||
Rückruf | Die durch Funktionstyp |
queue()
Der Rückgabewert der Funktion ist dasqueue()
Array/jQuery Typ
-Funktion eine Get-Operation oder eine Set-Operation ausführt. queue()
Wenn das aktuelle jQuery-Objekt mit mehreren Elementen übereinstimmt, verwendet die Funktion
beim Lesen von Daten nur das erste übereinstimmende Element.<span class="tag"><p</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"n1"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200px</span><span class="pun">;</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100px</span><span class="pun">;</span><span class="pln"> border</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1px</span><span class="pln"> solid </span><span class="com">#ccc;</span><span class="atv">"</span><span class="pln"> </span><span class="tag">></p></span><span class="pln"><br/></span><span class="tag"><p</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"n2"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200px</span><span class="pun">;</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100px</span><span class="pun">;</span><span class="pln"> border</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1px</span><span class="pln"> solid </span><span class="com">#ccc;</span><span class="atv">"</span><span class="pln"> </span><span class="tag">></p></span><span class="pln"><br/></span><span class="tag"><p</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"n3"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200px</span><span class="pun">;</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100px</span><span class="pun">;</span><span class="pln"> border</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1px</span><span class="pln"> solid </span><span class="com">#ccc;</span><span class="atv">"</span><span class="pln"> </span><span class="tag">></p></span>
Beispiel & Beschreibung
<span class="kwd">var</span><span class="pln"> $ps </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"p"</span><span class="pun">);</span><span class="pln"><br/><br/></span><span class="com">// 为每个p元素上的队列"q"设置(替换成)新的队列</span><span class="pln"><br/></span><span class="com">// (由于之前没有队列"q",这相当于新增一个对垒"q")</span><span class="pln"><br/>$ps</span><span class="pun">.</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">,</span><span class="pln"> </span><span class="pun">[</span><span class="pln"><br/> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">next</span><span class="pun">){</span><span class="pln"> alert</span><span class="pun">(</span><span class="str">"队列函数1"</span><span class="pun">);</span><span class="pln"> </span><span class="com">/* next(); 调用该函数可以移除并执行当前队列中的第一个函数 */</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">,</span><span class="pln"><br/> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">next</span><span class="pun">){</span><span class="pln"> alert</span><span class="pun">(</span><span class="str">"队列函数2"</span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">,</span><span class="pln"><br/> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">next</span><span class="pun">){</span><span class="pln"> alert</span><span class="pun">(</span><span class="str">"队列函数3"</span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span><span class="pln"><br/></span><span class="pun">]);</span><span class="pln"><br/><br/></span><span class="kwd">var</span><span class="pln"> queue </span><span class="pun">=</span><span class="pln"> $ps</span><span class="pun">.</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"> </span><span class="com">// 获取第一个p元素的队列"q"</span><span class="pln"><br/></span><span class="kwd">var</span><span class="pln"> queue1 </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"#n1"</span><span class="pun">).</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"><br/></span><span class="kwd">var</span><span class="pln"> queue2 </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"#n2"</span><span class="pun">).</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"><br/></span><span class="kwd">var</span><span class="pln"> queue3 </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"#n3"</span><span class="pun">).</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"><br/><a href="http://www.php.cn/code/658.html" target="_blank">document</a></span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue </span><span class="pun">===</span><span class="pln"> queue1 </span><span class="pun">);</span><span class="pln"> </span><span class="com">// true</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue </span><span class="pun">===</span><span class="pln"> queue2 </span><span class="pun">);</span><span class="pln"> </span><span class="com">// false</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue </span><span class="pun">===</span><span class="pln"> queue3 </span><span class="pun">);</span><span class="pln"> </span><span class="com">// false</span><span class="pln"><br/><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue</span><span class="pun">.</span><span class="pln">length </span><span class="pun">);</span><span class="pln"> </span><span class="com">// 3</span><span class="pln"><br/><br/></span><span class="com">// 为n1的队列"q"的末尾添加一个处理函数</span><span class="pln"><br/>$</span><span class="pun">(</span><span class="str">"#n1"</span><span class="pun">).</span><span class="pln">queue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(){</span><span class="pln"><br/> </span><span class="com">// 这里的this表示当前DOM元素(n1)</span><span class="pln"><br/> alert</span><span class="pun">(</span><span class="str">"队列函数4"</span><span class="pun">);</span><span class="pln"> <br/></span><span class="pun">});</span><span class="pln"><br/><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> queue</span><span class="pun">.</span><span class="pln">length </span><span class="pun">);</span><span class="pln"> </span><span class="com">// 4</span><span class="pln"><br/><br/></span><span class="com">// 使用dequeue()可以移除并执行队列中的第一个函数</span><span class="pln"><br/>$</span><span class="pun">(</span><span class="str">"#n1"</span><span class="pun">).</span><span class="pln">dequeue</span><span class="pun">(</span><span class="str">"q"</span><span class="pun">);</span><span class="pln"> </span><span class="com">// 弹出对话框:"队列函数1"</span>Nehmen Sie den folgenden HTML-Code als Beispiel:
queueName
<span class="kwd">var</span><span class="pln"> $ps </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">"p"</span><span class="pun">);</span><span class="pln"><br/><br/></span><span class="com">// 为每个p元素设置两个自定义动画</span><span class="pln"><br/>$ps</span><span class="pun">.</span><span class="pln">animate</span><span class="pun">(</span><span class="pln"> </span><span class="pun">{</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">400</span><span class="pun">,</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200</span><span class="pun">},</span><span class="pln"> </span><span class="lit">1000</span><span class="pln"> </span><span class="pun">)</span><span class="pln"><br/></span><span class="pun">.</span><span class="pln">animate</span><span class="pun">(</span><span class="pln"> </span><span class="pun">{</span><span class="pln">width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200</span><span class="pun">,</span><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100</span><span class="pln"> </span><span class="pun">},</span><span class="pln"> </span><span class="lit">1000</span><span class="pln"> </span><span class="pun">);</span><span class="pln"><br/><br/></span><span class="kwd">var</span><span class="pln"> fx </span><span class="pun">=</span><span class="pln"> $ps</span><span class="pun">.</span><span class="pln">queue</span><span class="pun">();</span><span class="pln"> </span><span class="com">// 相当于:var fx = $ps.queue("fx");</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> fx</span><span class="pun">.</span><span class="pln">length </span><span class="pun">);</span><span class="pln"> </span><span class="com">// 2</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> fx</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'<br>'</span><span class="pun">);</span><span class="pln"> </span><span class="com">// "inprogress"(第一个动画函数已被移除并开始执行,所以在队列开头添加该<a href="http://www.php.cn/wiki/57.html" target="_blank">字符串</a>来表示,如果执行完成,将开始移除并执行第二个动画函数)</span><span class="pln"><br/>document</span><span class="pun">.</span><span class="pln">writeln</span><span class="pun">(</span><span class="pln"> fx</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> </span><span class="pun">);</span><span class="pln"> </span><span class="com">// 第二个动画的执行函数</span><span class="pln"><br/><br/></span><span class="com">// 用一个空的数组替换当前动画队列,即可清空动画队列</span><span class="pln"><br/></span><span class="com">// 此时第一个动画函数已经从队列中移除、正在执行</span><span class="pln"><br/></span><span class="com">// 因此第一个动画函数执行完毕后,就不会执行第二个动画函数(被清空了)</span><span class="pln"><br/>$ps</span><span class="pun">.</span><span class="pln">queue</span><span class="pun">(</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="pun">]</span><span class="pln"> </span><span class="pun">);</span>Wir Außerdem müssen Sie den Parameter nicht angeben. Der Standardwert dieses Parameters ist „fx“, der die Standardeffektwarteschlange von jQuery darstellt.
Das obige ist der detaillierte Inhalt vonDetaillierte Erläuterung der Verwendung der jQuery.queue()-Instanz. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!