Làm cách nào để tạo vòng lặp trong HTML?

Vòng lặp for tiêu chuẩn trong JavaScript đếm các giá trị từ 1 đến 10. Nút “đếm đến mười” kích hoạt chức năng count() cho lập trình HTML5 và CSS3 của bạn. Đây là mã cho count() .

 function count(){
  output.innerHTML = ";
  for (i = 1; i <= 10; i++){
  output.innerHTML += i + "<br />";
  } // end for loop
 } // end count

Mặc dù hàm count() in rõ ràng mười dòng, nhưng nó chỉ có một dòng sửa đổi output div. The main code repeats many times to create the long output.

  1. Bạn có thể sử dụng đầu ra var ngay lập tức.

    đầu ra là một biến toàn cục và nó đã được tạo nên bạn có thể sử dụng nó ngay lập tức. Không cần khởi tạo nó trong hàm.

  2. Xóa đầu ra

    Đặt đầu ra. value vào chuỗi rỗng ( “” ) để xóa đầu ra. Điều này sẽ hủy bất kỳ văn bản nào hiện có trong div.

  3. Bắt đầu vòng lặp for .

    Vòng lặp for là một vòng lặp đặc biệt được sử dụng để lặp lại điều gì đó với số lần nhất định. Đối với các vòng lặp có ba thành phần. khởi tạo, so sánh và cập nhật.

  4. Khởi tạo biến đếm của bạn

    A for hoạt động bằng cách thay đổi giá trị của một số nguyên nhiều lần. Phần đầu tiên của vòng lặp for khởi tạo biến này (thường được gọi là i .

  5. Chỉ định một điều kiện để ở trong vòng lặp

    Phần thứ hai của câu lệnh a for là một điều kiện. Khi điều kiện còn đúng, vòng lặp sẽ tiếp tục. Ngay khi điều kiện được đánh giá là sai, vòng lặp sẽ thoát.

  6. Thay đổi biến

    Phần thứ ba của câu lệnh a for bằng cách nào đó thay đổi biến đếm. Cách phổ biến nhất để thay đổi biến là thêm một biến vào đó. Cú pháp i++ là lối tắt cho “thêm một vào i. ”

  7. Xây dựng một khối mã cho mã lặp lại

    Sử dụng dấu ngoặc nhọn và thụt đầu dòng để cho biết mã nào lặp lại. Tất cả mã bên trong dấu ngoặc nhọn lặp lại

  8. Bên trong vòng lặp, ghi vào đầu ra

    Trên mỗi lần lặp của vòng lặp, hãy thêm giá trị hiện tại của vào innerHTML của div đầu ra. Đồng thời thêm dấu ngắt (
    ) để làm cho đầu ra trông đẹp hơn. Khi bạn thêm vào một thuộc tính innerHTML , bạn đang viết mã HTML, vì vậy nếu bạn muốn đầu ra xuất hiện trên các dòng khác nhau, bạn cần viết .

  9. Đóng vòng lặp

    Đừng quên kết thúc vòng lặp, nếu không chương trình của bạn sẽ không chạy chính xác

Vòng lặp cung cấp một cách nhanh chóng và dễ dàng để làm điều gì đó lặp đi lặp lại. Chương này của Hướng dẫn JavaScript giới thiệu các câu lệnh lặp khác nhau có sẵn cho JavaScript

Bạn có thể coi vòng lặp là một phiên bản vi tính hóa của trò chơi trong đó bạn yêu cầu ai đó thực hiện X bước theo một hướng, sau đó Y bước theo hướng khác. Ví dụ: ý tưởng "Đi năm bước về phía đông" có thể được diễn đạt theo cách này dưới dạng một vòng lặp

for (let step = 0; step < 5; step++) {
  // Runs 5 times, with values of step 0 through 4.
  console.log('Walking east one step');
}

Có nhiều loại vòng lặp khác nhau, nhưng về cơ bản chúng đều làm cùng một việc. họ lặp lại một hành động một số lần. (Lưu ý rằng có thể số đó có thể bằng không. )

Các cơ chế vòng lặp khác nhau cung cấp các cách khác nhau để xác định điểm bắt đầu và điểm kết thúc của vòng lặp. Có nhiều tình huống dễ dàng được phục vụ bởi một loại vòng lặp hơn các loại vòng lặp khác

Các câu lệnh cho các vòng lặp được cung cấp trong JavaScript là

Một vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
3 lặp lại cho đến khi một điều kiện cụ thể đánh giá là sai. Vòng lặp JavaScript
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
3 tương tự như vòng lặp
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
3 của Java và C

Một câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
3 trông như sau

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement

Khi một vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
3 thực thi, điều sau xảy ra

  1. Biểu thức khởi tạo
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    8, nếu có, được thực thi. Biểu thức này thường khởi tạo một hoặc nhiều bộ đếm vòng lặp, nhưng cú pháp cho phép biểu thức ở bất kỳ mức độ phức tạp nào. Biểu thức này cũng có thể khai báo biến
  2. Biểu thức
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    9 được đánh giá. Nếu giá trị của
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    9 là true, các câu lệnh lặp sẽ thực thi. Ngược lại, vòng lặp
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    3 kết thúc. (Nếu bỏ hoàn toàn biểu thức
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    9 thì điều kiện được coi là đúng. )
  3. function countSelected(selectObject) {
      let numberSelected = 0;
      for (let i = 0; i < selectObject.options.length; i++) {
        if (selectObject.options[i].selected) {
          numberSelected++;
        }
      }
      return numberSelected;
    }
    
    const btn = document.getElementById('btn');
    
    btn.addEventListener('click', () => {
      const musicTypes = document.selectForm.musicTypes;
      console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
    });
    
    3 thực hiện. Để thực hiện nhiều câu lệnh, hãy sử dụng câu lệnh khối (
    function countSelected(selectObject) {
      let numberSelected = 0;
      for (let i = 0; i < selectObject.options.length; i++) {
        if (selectObject.options[i].selected) {
          numberSelected++;
        }
      }
      return numberSelected;
    }
    
    const btn = document.getElementById('btn');
    
    btn.addEventListener('click', () => {
      const musicTypes = document.selectForm.musicTypes;
      console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
    });
    
    4) để nhóm các câu lệnh đó
  4. Nếu có, biểu thức cập nhật
    function countSelected(selectObject) {
      let numberSelected = 0;
      for (let i = 0; i < selectObject.options.length; i++) {
        if (selectObject.options[i].selected) {
          numberSelected++;
        }
      }
      return numberSelected;
    }
    
    const btn = document.getElementById('btn');
    
    btn.addEventListener('click', () => {
      const musicTypes = document.selectForm.musicTypes;
      console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
    });
    
    5 được thực thi
  5. Điều khiển quay lại Bước 2

Trong ví dụ bên dưới, hàm chứa câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
3 đếm số tùy chọn đã chọn trong danh sách cuộn (phần tử
function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
7 cho phép nhiều lựa chọn)

HTML

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>

JavaScript

Ở đây, câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
3 khai báo biến
function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
9 và khởi tạo nó thành
do
  statement
while (condition);
0. Nó kiểm tra xem
function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
9 có ít hơn số tùy chọn trong phần tử
function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
7 hay không, thực hiện câu lệnh
do
  statement
while (condition);
3 tiếp theo và tăng
function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
9 lên 1 sau mỗi lần đi qua vòng lặp

function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});

Câu lệnh

do
  statement
while (condition);
5 lặp lại cho đến khi một điều kiện cụ thể được đánh giá là sai

Một câu lệnh

do
  statement
while (condition);
5 trông như sau

do
  statement
while (condition);

function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
3 luôn được thực hiện một lần trước khi điều kiện được kiểm tra. (Để thực hiện nhiều câu lệnh, hãy sử dụng câu lệnh khối (______13_______4) để nhóm các câu lệnh đó. )

Nếu

do
  statement
while (condition);
9 là
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
0, câu lệnh sẽ thực hiện lại. Vào cuối mỗi lần thực hiện, điều kiện được kiểm tra. Khi điều kiện là
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
1, quá trình thực thi dừng lại và quyền điều khiển chuyển sang câu lệnh theo sau
do
  statement
while (condition);
5

Trong ví dụ sau, vòng lặp

let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
3 lặp lại ít nhất một lần và lặp lại cho đến khi
function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
9 không còn nhỏ hơn
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
5

let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);

Một câu lệnh

let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
6 thực hiện các câu lệnh của nó miễn là một điều kiện cụ thể đánh giá bằng
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
0. Một câu lệnh
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
6 trông như sau

while (condition)
  statement

Nếu

do
  statement
while (condition);
9 trở thành
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
1, thì ____13_______3 trong vòng lặp sẽ dừng thực thi và quyền điều khiển được chuyển đến câu lệnh sau vòng lặp

Kiểm tra điều kiện xảy ra trước khi thực thi

function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
3 trong vòng lặp. Nếu điều kiện trả về
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
0, thì ____13_______3 được thực hiện và ______21_______9 được kiểm tra lại. Nếu điều kiện trả về
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
1, quá trình thực thi sẽ dừng lại và quyền điều khiển được chuyển đến câu lệnh theo sau
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
6

Để thực hiện nhiều câu lệnh, hãy sử dụng câu lệnh khối (

function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
4) để nhóm các câu lệnh đó

Vòng lặp

let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
6 sau đây lặp lại miễn là
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
0 nhỏ hơn
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
1

let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}

Với mỗi lần lặp lại, vòng lặp tăng lên

let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
0 và thêm giá trị đó vào
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
3. Do đó,
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
3 và
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
0 nhận các giá trị sau

  • Sau lần vượt qua đầu tiên.
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    0 =
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    7 và
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    3 =
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    7
  • Sau khi vượt qua lần thứ hai.
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    0 =
    // Infinite loops are bad!
    while (true) {
      console.log('Hello, world!');
    }
    
    1 và
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    3 =
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    1
  • Sau khi vượt qua lần thứ ba.
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    0 =
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    1 và
    let n = 0;
    let x = 0;
    while (n < 3) {
      n++;
      x += n;
    }
    
    3 =
    // Infinite loops are bad!
    while (true) {
      console.log('Hello, world!');
    }
    
    7

Sau khi hoàn thành bước thứ ba, điều kiện

// Infinite loops are bad!
while (true) {
  console.log('Hello, world!');
}
8 không còn là
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
0, do đó vòng lặp kết thúc

Tránh các vòng lặp vô hạn. Đảm bảo điều kiện trong một vòng lặp cuối cùng trở thành

let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
1—nếu không, vòng lặp sẽ không bao giờ kết thúc. Các câu lệnh trong vòng lặp
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
6 sau đây sẽ thực thi vĩnh viễn vì điều kiện không bao giờ trở thành
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
1

// Infinite loops are bad!
while (true) {
  console.log('Hello, world!');
}

Một

label:
  statement
3 cung cấp một câu lệnh có mã định danh cho phép bạn tham khảo nó ở nơi khác trong chương trình của mình. Ví dụ: bạn có thể sử dụng nhãn để xác định một vòng lặp, sau đó sử dụng các câu lệnh
label:
  statement
4 hoặc
label:
  statement
5 để cho biết chương trình nên ngắt vòng lặp hay tiếp tục thực hiện vòng lặp đó

Cú pháp của câu lệnh có nhãn giống như sau

label:
  statement

Giá trị của

label:
  statement
3 có thể là bất kỳ mã định danh JavaScript nào không phải là từ dành riêng.
function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
3 mà bạn xác định bằng nhãn có thể là bất kỳ tuyên bố nào

Trong ví dụ này, nhãn

label:
  statement
8 xác định một vòng lặp
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
6

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
0

Sử dụng câu lệnh

label:
  statement
4 để kết thúc vòng lặp,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
01 hoặc kết hợp với câu lệnh có nhãn

  • Khi bạn sử dụng
    label:
      statement
    
    4 mà không có nhãn, nó sẽ chấm dứt phần bên trong cùng của
    let i = 0;
    do {
      i += 1;
      console.log(i);
    } while (i < 5);
    
    6,
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    04,
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    3 hoặc
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    01 ngay lập tức và chuyển quyền kiểm soát sang câu lệnh sau
  • Khi bạn sử dụng
    label:
      statement
    
    4 với một nhãn, nó sẽ chấm dứt câu lệnh được gắn nhãn đã chỉ định

Cú pháp của câu lệnh

label:
  statement
4 trông như thế này

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
1

  1. Dạng đầu tiên của cú pháp kết thúc vòng lặp bao quanh trong cùng hoặc
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    01
  2. Dạng thứ hai của cú pháp kết thúc câu lệnh có nhãn kèm theo đã chỉ định

Ví dụ sau lặp qua các phần tử trong một mảng cho đến khi tìm thấy chỉ mục của phần tử có giá trị là

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
10

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
2

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
3

Câu lệnh

label:
  statement
5 có thể được sử dụng để bắt đầu lại câu lệnh
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
6,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
04,
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
3 hoặc
label:
  statement
3

  • Khi bạn sử dụng
    label:
      statement
    
    5 mà không có nhãn, nó sẽ chấm dứt vòng lặp hiện tại của câu lệnh bao quanh trong cùng của
    let i = 0;
    do {
      i += 1;
      console.log(i);
    } while (i < 5);
    
    6,
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    04 hoặc
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    3 và tiếp tục thực hiện vòng lặp với lần lặp tiếp theo. Ngược lại với câu lệnh
    label:
      statement
    
    4,
    label:
      statement
    
    5 không chấm dứt hoàn toàn việc thực hiện vòng lặp. Trong một vòng lặp
    let i = 0;
    do {
      i += 1;
      console.log(i);
    } while (i < 5);
    
    6, nó nhảy trở lại điều kiện. Trong vòng lặp
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:</label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&B</option>
        <option>Jazz</option>
        <option>Blues</option>
        <option>New Age</option>
        <option>Classical</option>
        <option>Opera</option>
      </select>
      <button id="btn" type="button">How many are selected?</button>
    </form>
    
    3, nó nhảy tới vòng lặp
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    24
  • Khi bạn sử dụng
    label:
      statement
    
    5 với nhãn, nó sẽ áp dụng cho câu lệnh lặp được xác định bằng nhãn đó

Cú pháp của câu lệnh

label:
  statement
5 như sau

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
4

Ví dụ sau đây cho thấy một vòng lặp

let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
6 với câu lệnh
label:
  statement
5 thực thi khi giá trị của
function countSelected(selectObject) {
  let numberSelected = 0;
  for (let i = 0; i < selectObject.options.length; i++) {
    if (selectObject.options[i].selected) {
      numberSelected++;
    }
  }
  return numberSelected;
}

const btn = document.getElementById('btn');

btn.addEventListener('click', () => {
  const musicTypes = document.selectForm.musicTypes;
  console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
9 là
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
1. Do đó,
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
0 nhận các giá trị
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
7,
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
1,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
34 và
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
35

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
5

Nếu bạn nhận xét

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
36, vòng lặp sẽ chạy cho đến hết và bạn sẽ thấy
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
37

Một câu lệnh có nhãn

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38 chứa một câu lệnh có nhãn
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
39. Nếu gặp phải
label:
  statement
5, chương trình sẽ kết thúc lần lặp hiện tại của
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
39 và bắt đầu lần lặp tiếp theo. Mỗi lần gặp phải
label:
  statement
5,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
39 lặp lại cho đến khi điều kiện của nó trở lại
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
1. Khi
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
1 được trả về, phần còn lại của câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38 được hoàn thành và ____6_______38 nhắc lại cho đến khi điều kiện của nó trả về ____33_______1. Khi
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 5);
1 được trả về, chương trình tiếp tục tại câu lệnh sau
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38

Nếu

label:
  statement
5 có nhãn là
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38, chương trình sẽ tiếp tục ở đầu câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
6

Câu lệnh

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 lặp lại một biến được chỉ định trên tất cả các thuộc tính có thể đếm được của một đối tượng. Đối với mỗi thuộc tính riêng biệt, JavaScript thực thi các câu lệnh đã chỉ định. Một câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 trông như sau

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
7

Hàm sau đây nhận đối số của nó là một đối tượng và tên của đối tượng. Sau đó, nó lặp lại trên tất cả các thuộc tính của đối tượng và trả về một chuỗi liệt kê tên thuộc tính và giá trị của chúng

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
8

Đối với một đối tượng

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
56 với các thuộc tính
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
57 và
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
58, thì
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
59 sẽ là

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
9

Mặc dù có thể muốn sử dụng điều này như một cách để lặp lại các phần tử

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
60, nhưng câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 sẽ trả về tên của các thuộc tính do người dùng xác định bên cạnh các chỉ mục số

Do đó, tốt hơn là sử dụng vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
3 truyền thống với chỉ mục số khi lặp qua các mảng, bởi vì câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 lặp qua các thuộc tính do người dùng xác định ngoài các phần tử mảng, nếu bạn sửa đổi đối tượng Array (chẳng hạn như thêm các thuộc tính tùy chỉnh hoặc

Câu lệnh

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
64 tạo một vòng lặp Lặp lại trên các đối tượng có thể lặp lại (bao gồm đối tượng
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
60,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
66,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
67,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
68, v.v.), gọi một hook lặp tùy chỉnh với các câu lệnh sẽ được thực thi cho giá trị của từng thuộc tính riêng biệt

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
0

Ví dụ sau đây cho thấy sự khác biệt giữa vòng lặp

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
64 và vòng lặp
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54. Trong khi
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 lặp lại tên thuộc tính, thì
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
64 lặp lại giá trị thuộc tính

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:</label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&B</option>
    <option>Jazz</option>
    <option>Blues</option>
    <option>New Age</option>
    <option>Classical</option>
    <option>Opera</option>
  </select>
  <button id="btn" type="button">How many are selected?</button>
</form>
1

Các câu lệnh

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
64 và
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 cũng có thể được sử dụng với hàm hủy. Ví dụ: bạn có thể lặp đồng thời các khóa và giá trị của một đối tượng bằng cách sử dụng
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
75

Bạn có thể viết các vòng lặp trong HTML không?

Cách tiếp cận 1. Sử dụng vòng lặp for. Các phần tử HTML có thể được lặp lại bằng cách sử dụng vòng lặp JavaScript thông thường . Số lượng phần tử được lặp lại có thể được tìm thấy bằng cách sử dụng thuộc tính độ dài. Vòng lặp for có ba phần, phần khởi tạo, biểu thức điều kiện và biểu thức tăng/giảm.

Vòng lặp for bắt đầu trong HTML như thế nào?

Vòng lặp bắt đầu ở vị trí 0 ( let i = 0 ).

Làm cách nào để lặp div trong HTML?

createDocumentFragment(); . createElement('div'); . id = 'r'+i; . className = 'ansbox'; . appendChild(newDiv);