Hướng dẫn select all elements in array javascript - chọn tất cả các phần tử trong mảng javascript

Bạn không thể thêm một lớp vào tất cả các yếu tố mảng cùng một lúc, bất kể kỹ thuật bạn chọn bạn phải lặp lại trên mảng; May mắn thay, JavaScript có một số cách để lặp lại các mảng.

Nếu bạn có thể sử dụng ES6, bạn có quyền truy cập vào Array.from[] và các hàm mũi tên, có thể được sử dụng như sau:

function hideButtons[]{

  // document.querySelectorAll[] retrieves all elements
  // matching the supplied CSS selector, and returns
  // a non-live NodeList:
  var buttons = document.querySelectorAll['.buttons'];

  // Array.from[] converts the supplied Array-like object
  // into an Array, enabling the use of Array methods:
  Array.from[ buttons ]

    // here button represents the current element
    // of the Array of buttons over which we're iterating,
    // the expression following the fat arrow ['=>']
    // is executed on each iteration, and adds the 'hidden'
    // class-name to each element of the Array:
    .forEach[button => button.addClass['hidden'];
}

Không có ES6, bạn có thể tạo lại hành vi trên với các điều sau:

function hideButtons[]{
  var buttons = document.querySelectorAll['.buttons'],

      // here we use Function.prototype.call[] to allow
      // us to apply the Array.prototype.slice to the
      // NodeList, creating an Array from the NodeList:
      buttonArray = Array.prototype.slice.call[buttons, 0];

  // here we use Array.prototype.forEach[], with its
  // anonymous function:
  buttonArray.forEach[function[button] {
    // 'button' again refers to the current
    // element of the array of elements over
    // which we're iterating.

    // here we add the 'hidden' class-name to
    // each element of the array over which
    // we're iterating:
    button.classList.add['hidden'];
  }];
};

References:

  • Array.from[].
  • Array.prototype.forEach[].
  • Array.prototype.slice[].
  • Hàm mũi tên.
  • document.querySelectorAll[].
  • Element.classList.
  • function.prototype.call[].

9-11

ví dụ 1

Kiểm tra xem tất cả các giá trị ở độ tuổi [] là trên 18:

const lứa tuổi = [32, 33, 16, 40];

Tuổi. Mọi người [kiểm tra]
  return age > 18;
}

Kiểm tra chức năng [tuổi] {& nbsp; Trở về tuổi> 18;}

Hãy tự mình thử »

Thêm ví dụ dưới đây.

Định nghĩa và cách sử dụng

Phương thức every[] thực thi một hàm cho mỗi phần tử mảng.

Phương thức every[] trả về

function hideButtons[]{
  var buttons = document.querySelectorAll['.buttons'],

      // here we use Function.prototype.call[] to allow
      // us to apply the Array.prototype.slice to the
      // NodeList, creating an Array from the NodeList:
      buttonArray = Array.prototype.slice.call[buttons, 0];

  // here we use Array.prototype.forEach[], with its
  // anonymous function:
  buttonArray.forEach[function[button] {
    // 'button' again refers to the current
    // element of the array of elements over
    // which we're iterating.

    // here we add the 'hidden' class-name to
    // each element of the array over which
    // we're iterating:
    button.classList.add['hidden'];
  }];
};
1 nếu hàm trả về đúng cho tất cả các phần tử.

Phương thức every[] trả về

function hideButtons[]{
  var buttons = document.querySelectorAll['.buttons'],

      // here we use Function.prototype.call[] to allow
      // us to apply the Array.prototype.slice to the
      // NodeList, creating an Array from the NodeList:
      buttonArray = Array.prototype.slice.call[buttons, 0];

  // here we use Array.prototype.forEach[], with its
  // anonymous function:
  buttonArray.forEach[function[button] {
    // 'button' again refers to the current
    // element of the array of elements over
    // which we're iterating.

    // here we add the 'hidden' class-name to
    // each element of the array over which
    // we're iterating:
    button.classList.add['hidden'];
  }];
};
3 nếu hàm trả về sai cho một phần tử.

Phương thức every[] không thực thi hàm cho các phần tử trống.

Phương thức every[] không thay đổi mảng gốc

Cú pháp

Array.very [function [currentValue, index, mảng], điều này]

Thông sốTham số
Sự mô tảhàm số[]
A function to be run for each element in the array.
Yêu cầu.a hàm được chạy cho từng phần tử trong mảng.Hiện tại giá trị
The value of the current element.
Yêu cầu. Giá trị của phần tử hiện tại.mục lục
The index of the current element.
Tùy chọn. Chỉ số của phần tử hiện tại.mảng
The array of the current element.
Tùy chọn. Mảng của phần tử hiện tại.giá trị này
A value passed to the function as its
function hideButtons[]{
  var buttons = document.querySelectorAll['.buttons'],

      // here we use Function.prototype.call[] to allow
      // us to apply the Array.prototype.slice to the
      // NodeList, creating an Array from the NodeList:
      buttonArray = Array.prototype.slice.call[buttons, 0];

  // here we use Array.prototype.forEach[], with its
  // anonymous function:
  buttonArray.forEach[function[button] {
    // 'button' again refers to the current
    // element of the array of elements over
    // which we're iterating.

    // here we add the 'hidden' class-name to
    // each element of the array over which
    // we're iterating:
    button.classList.add['hidden'];
  }];
};
7 value.

Không bắt buộc. Mặc định ________ 16.a giá trị được truyền đến hàm là giá trị
function hideButtons[]{
  var buttons = document.querySelectorAll['.buttons'],

      // here we use Function.prototype.call[] to allow
      // us to apply the Array.prototype.slice to the
      // NodeList, creating an Array from the NodeList:
      buttonArray = Array.prototype.slice.call[buttons, 0];

  // here we use Array.prototype.forEach[], with its
  // anonymous function:
  buttonArray.forEach[function[button] {
    // 'button' again refers to the current
    // element of the array of elements over
    // which we're iterating.

    // here we add the 'hidden' class-name to
    // each element of the array over which
    // we're iterating:
    button.classList.add['hidden'];
  }];
};
7 của nó.

Giá trị trả lạiTham số
Sự mô tảhàm số[]

Yêu cầu.a hàm được chạy cho từng phần tử trong mảng.

Hiện tại giá trị

Yêu cầu. Giá trị của phần tử hiện tại.
  { name: "Steve", answer: "Yes"},
  { name: "Jessica", answer: "Yes"},
  { name: "Peter", answer: "Yes"},
  { name: "Elaine", answer: "No"}
];

mục lục

Tùy chọn. Chỉ số của phần tử hiện tại.
  if [index === 0] {
    return true;
  } else {
    return [el.answer === arr[index - 1].answer];
  }
}

Kiểm tra chức năng [tuổi] {& nbsp; Trở về tuổi> 18;}

Hãy tự mình thử »

Thêm ví dụ dưới đây.

Định nghĩa và cách sử dụng

Thêm ví dụ dưới đây.

Định nghĩa và cách sử dụng
const ages = [32, 33, 12, 40];

Phương thức every[] thực thi một hàm cho mỗi phần tử mảng.
  return age > document.getElementById["ageToCheck"].value;
}

Phương thức every[] trả về

function hideButtons[]{
  var buttons = document.querySelectorAll['.buttons'],

      // here we use Function.prototype.call[] to allow
      // us to apply the Array.prototype.slice to the
      // NodeList, creating an Array from the NodeList:
      buttonArray = Array.prototype.slice.call[buttons, 0];

  // here we use Array.prototype.forEach[], with its
  // anonymous function:
  buttonArray.forEach[function[button] {
    // 'button' again refers to the current
    // element of the array of elements over
    // which we're iterating.

    // here we add the 'hidden' class-name to
    // each element of the array over which
    // we're iterating:
    button.classList.add['hidden'];
  }];
};
1 nếu hàm trả về đúng cho tất cả các phần tử.
  document.getElementById["demo"].innerHTML = ages.every[checkAge];
}

Kiểm tra chức năng [tuổi] {& nbsp; Trở về tuổi> 18;}

Hãy tự mình thử »

Thêm ví dụ dưới đây.

Định nghĩa và cách sử dụng

Trình duyệt ChromeI EBờ rìaFirefoxCuộc đi sănOpera
Đúng9-11ĐúngĐúngĐúngĐúng

Bài Viết Liên Quan

Chủ Đề