Hướng dẫn html loop through elements - html vòng lặp qua các phần tử

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luậnUsing the for loop: The HTML elements can be iterated by using the regular JavaScript for loop. The number of elements to be iterated can be found using the length property. The for loop has three parts, initialization, condition expression, and increment/decrement expression. Each of the items can be accessed by using square brackets with their respective index.

    Syntax:

    for (let i = 0; i < elements.length; i++) {
        console.log(elements[i]);
    }
    

    Example:

    Trong bài viết này, chúng ta sẽ học cách lặp qua các phần tử HTML mà không cần sử dụng phương thức foreach ().

    <html>

    <body>

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    3
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2>

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    9
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2>

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2
    for (element of elements) {
        console.log(element);
    }
    5
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2>

    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 JavaScript thông thường cho vòng lặp. Số lượng các 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 chiều dài. Vòng lặp cho ba phần, khởi tạo, biểu thức điều kiện và biểu thức tăng/giảm. Mỗi mục có thể được truy cập bằng cách sử dụng dấu ngoặc vuông với chỉ mục tương ứng của chúng.

    56

    58

    9<0

    5<2

    5<4<5<6

    HTML

    9html0

    5html2

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0html4

    html5body>

    html5html>

    Output:

    Hướng dẫn html loop through elements - html vòng lặp qua các phần tử

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<0 123>Using the While loop: The HTML elements can be iterated by using a regular JavaScript while loop. The number of elements can be found using the length property. A temporary value is used to keep track of the current iteration by checking it in the condition expression. Each of the items can then be accessed by using square brackets with their respective index.

    Syntax:

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }

    Example:

    Trong bài viết này, chúng ta sẽ học cách lặp qua các phần tử HTML mà không cần sử dụng phương thức foreach ().

    <html>

    <body>

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    3
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2>

    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 JavaScript thông thường cho vòng lặp. Số lượng các 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 chiều dài. Vòng lặp cho ba phần, khởi tạo, biểu thức điều kiện và biểu thức tăng/giảm. Mỗi mục có thể được truy cập bằng cách sử dụng dấu ngoặc vuông với chỉ mục tương ứng của chúng.

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2
    for (element of elements) {
        console.log(element);
    }
    5
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2>

    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 JavaScript thông thường cho vòng lặp. Số lượng các 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 chiều dài. Vòng lặp cho ba phần, khởi tạo, biểu thức điều kiện và biểu thức tăng/giảm. Mỗi mục có thể được truy cập bằng cách sử dụng dấu ngoặc vuông với chỉ mục tương ứng của chúng.

    56

    58

    9<0

    5

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    00

    5

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    02

    5

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    04

    5

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    06

    5

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    08

    5

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    10<5
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    12

    9<8

    9html0

    9

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    18

    9

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    20

    5html2

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0html4

    HTML

    html5html>

    Output:

    Hướng dẫn html loop through elements - html vòng lặp qua các phần tử

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<0 123> Using the ‘for…..of’ statement: The for…of statement can be used to loop over values of an iterable object. It includes objects like an Array, Map, Set, or HTML elements. A temporary variable holds the current value during the execution of the loop, which can then be used in the body of the loop.

    Syntax:

    for (element of elements) {
        console.log(element);
    }

    Example:

    Trong bài viết này, chúng ta sẽ học cách lặp qua các phần tử HTML mà không cần sử dụng phương thức foreach ().

    <html>

    <body>

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    3
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2>

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    9
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2>

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0<
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2
    for (element of elements) {
        console.log(element);
    }
    5
    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    2>

    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 JavaScript thông thường cho vòng lặp. Số lượng các 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 chiều dài. Vòng lặp cho ba phần, khởi tạo, biểu thức điều kiện và biểu thức tăng/giảm. Mỗi mục có thể được truy cập bằng cách sử dụng dấu ngoặc vuông với chỉ mục tương ứng của chúng.

    56

    HTML

    9<0

    5

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    70

    5

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    72

    5

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    74

    9<8

    9

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    78

    5html2

    let i = 0;
    while(i < elements.length) {
        console.log(elements[i]);
        i++;
    }
    0html50>

    html5body>

    html5html>

    Output:

    Hướng dẫn html loop through elements - html vòng lặp qua các phần tử