在jQuery中循环

时间:2014-11-27 06:57:35

标签: javascript 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中执行循环?

4 个答案:

答案 0 :(得分:7)

while loop

的情况下,javascript和jquery之间没有区别

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

答案 1 :(得分:1)

<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>

答案 2 :(得分:0)

在jQuery中也是如此。这是the documentation

jQuery中有一个流行的loop method,但它并不适用于您的示例。 $.each()允许您遍历数组和类似数组的对象。

答案 3 :(得分:0)

while循环中的javascript和jquery没有区别。

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

这将起作用。