簡體   English   中英

jQuery中的while循環

[英]while loop in jQuery

我需要使用while循環,我想知道jQuery中是否有任何語法更改

在JavaScript中

  var text = "";
    var i = 0;
    while (i < 10) {
        text += "<br>The number is " + i;
        i++;
    }
    document.getElementById("demo").innerHTML = text;

我怎樣才能像這樣在jquery中執行循環?

while loop ,javascript和jquery之間沒有區別

 $(function () { var text = ""; var i = 0; while (i < 10) { text += "<br>The number is " + i; i++; } $("#demo").html(text); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="demo"></div> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javescript">
    $(document).ready(function () {
    var text = "";
    var i = 0;
    while (i < 10) {
        text += "<br>The number is " + i;
        i++;
    }}); 
    </script>

在jQuery中將是相同的。 這是文檔

jQuery中有一個流行的循環方法 ,但不適用於您的示例。 $.each()允許您遍歷數組和類似數組的對象。

javascript和jquery while循環沒有區別。

$(document).ready(function () {
var text = "";
var i = 0;
while (i < 10) {
    text += "<br>The number is " + i;
    i++;
}}); 

這將起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM