Nhận tất cả các div bên trong div JavaScript

Thuộc tính điều hướng DOM tuyệt vời khi các phần tử gần nhau. Nếu họ không thì sao?

Có các phương pháp tìm kiếm bổ sung cho điều đó

Nếu một phần tử có thuộc tính

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3, chúng ta có thể lấy phần tử đó bằng cách sử dụng phương thức
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
4, bất kể nó ở đâu

Ví dụ

Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';

Ngoài ra, có một biến toàn cục được đặt tên bởi

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3 tham chiếu đến phần tử

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']

…Đó là trừ khi chúng ta khai báo một biến JavaScript có cùng tên, thì nó sẽ được ưu tiên

let elem = 5; // now elem is 5, not a reference to
alert[elem]; // 5

Vui lòng không sử dụng các biến toàn cục có tên id để truy cập các phần tử

Hành vi này được mô tả, nhưng nó được hỗ trợ chủ yếu để tương thích

Trình duyệt cố gắng giúp chúng tôi bằng cách trộn các không gian tên của JS và DOM. Điều đó tốt cho các tập lệnh đơn giản, được đưa vào HTML, nhưng nhìn chung không phải là điều tốt. Có thể có xung đột đặt tên. Ngoài ra, khi một người đọc mã JS và không có HTML trong chế độ xem, thì không rõ biến đó đến từ đâu

Ở đây trong hướng dẫn này, chúng tôi sử dụng

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3 để tham chiếu trực tiếp một phần tử cho ngắn gọn, khi rõ ràng phần tử đó đến từ đâu

Trong cuộc sống thực,

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
7 là phương pháp ưa thích

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3 phải là duy nhất

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3 phải là duy nhất. Chỉ có thể có một phần tử trong tài liệu với
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3 đã cho

Nếu có nhiều phần tử có cùng một

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3, thì hành vi của các phương thức sử dụng nó là không thể đoán trước, e. g.
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
7 có thể trả lại bất kỳ yếu tố nào như vậy một cách ngẫu nhiên. Vì vậy, hãy tuân thủ quy tắc và giữ cho
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3 là duy nhất

Chỉ

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
7, không phải
let elem = 5; // now elem is 5, not a reference to
alert[elem]; // 5
5

Phương thức

let elem = 5; // now elem is 5, not a reference to
alert[elem]; // 5
6 chỉ có thể được gọi trên đối tượng
let elem = 5; // now elem is 5, not a reference to
alert[elem]; // 5
7. Nó tìm kiếm
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3 đã cho trong toàn bộ tài liệu

Cho đến nay, phương thức linh hoạt nhất,

let elem = 5; // now elem is 5, not a reference to
alert[elem]; // 5
9 trả về tất cả các phần tử bên trong
Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
00 khớp với bộ chọn CSS đã cho

Ở đây chúng tôi tìm kiếm tất cả các phần tử

Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
01 là phần tử con cuối cùng

Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
0

Phương pháp này thực sự mạnh mẽ, bởi vì bất kỳ bộ chọn CSS nào cũng có thể được sử dụng

Cũng có thể sử dụng các lớp giả

Các lớp giả trong bộ chọn CSS như

Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
02 và
Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
03 cũng được hỗ trợ. Chẳng hạn,
Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
04 sẽ trả về tập hợp có các phần tử mà con trỏ hiện đang kết thúc [theo thứ tự lồng nhau. từ
Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
05 ngoài cùng đến cái lồng vào nhau nhất]

Lệnh gọi tới

Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
06 trả về phần tử đầu tiên cho bộ chọn CSS đã cho

Nói cách khác, kết quả giống như

Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
07, nhưng cái sau đang tìm kiếm tất cả các phần tử và chọn một phần tử, trong khi
Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
08 chỉ tìm kiếm một phần tử. Vì vậy, nó nhanh hơn và cũng ngắn hơn để viết

Các phương pháp trước đó là tìm kiếm DOM

Nó không tìm kiếm bất cứ thứ gì, nó chỉ kiểm tra xem

Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
00 có khớp với bộ chọn CSS đã cho hay không. Nó trả về
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
10 hoặc
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
11

Phương thức này rất hữu ích khi chúng ta đang lặp lại các phần tử [như trong một mảng hoặc thứ gì đó] và cố gắng lọc ra những phần tử mà chúng ta quan tâm

Ví dụ

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
1

Tổ tiên của một phần tử là. cha mẹ, cha mẹ của cha mẹ, cha mẹ của nó, v.v. Tổ tiên cùng nhau tạo thành chuỗi cha mẹ từ phần tử đến đỉnh

Phương thức

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
12 tìm tổ tiên gần nhất khớp với bộ chọn CSS. Bản thân
Element
// get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
00 cũng được đưa vào tìm kiếm

Nói cách khác, phương thức

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
14 đi lên từ phần tử và kiểm tra từng phần tử cha. Nếu nó khớp với bộ chọn, thì quá trình tìm kiếm sẽ dừng lại và tổ tiên được trả về

Ví dụ

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
7

Ngoài ra còn có các phương pháp khác để tìm kiếm các nút theo thẻ, lớp, v.v.

Ngày nay, chúng chủ yếu là lịch sử, vì

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
15 mạnh mẽ hơn và viết ngắn hơn

Vì vậy, ở đây chúng tôi trình bày chúng chủ yếu để hoàn thiện, trong khi bạn vẫn có thể tìm thấy chúng trong các tập lệnh cũ

  • Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    16 tìm kiếm các phần tử với thẻ đã cho và trả về bộ sưu tập của chúng. Tham số
    Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    17 cũng có thể là dấu sao
    Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    18 cho “bất kỳ thẻ nào”
  • Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    19 trả về các phần tử có lớp CSS đã cho
  • Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    70 trả về các phần tử có thuộc tính
    Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    71 đã cho, trên toàn bộ tài liệu. Rất hiếm khi được sử dụng

Ví dụ

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
5

Hãy tìm tất cả các thẻ

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
72 bên trong bảng

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
7

Đừng quên lá thư

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
73

Các nhà phát triển mới làm quen đôi khi quên chữ cái

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
73. Đó là, họ cố gắng gọi
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
75 thay vì
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
76

Chữ cái

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
73 không có trong
let elem = 5; // now elem is 5, not a reference to
alert[elem]; // 5
6, vì nó trả về một phần tử duy nhất. Nhưng
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
79 trả về một tập hợp các phần tử, vì vậy có
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
73 bên trong

Nó trả về một bộ sưu tập, không phải một phần tử

Một sai lầm phổ biến khác của người mới là viết

let elem = 5; // now elem is 5, not a reference to
alert[elem]; // 5
6

Điều đó sẽ không hiệu quả, bởi vì nó lấy một tập hợp các đầu vào và gán giá trị cho nó thay vì cho các phần tử bên trong nó

Chúng ta nên lặp lại bộ sưu tập hoặc lấy một phần tử theo chỉ mục của nó, sau đó gán, như thế này

let elem = 5; // now elem is 5, not a reference to
alert[elem]; // 5
7

Tìm kiếm các phần tử

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
51

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
0

Tất cả các phương pháp

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
52 trả về một bộ sưu tập trực tiếp. Các bộ sưu tập như vậy luôn phản ánh trạng thái hiện tại của tài liệu và “tự động cập nhật” khi nó thay đổi

Trong ví dụ bên dưới, có hai tập lệnh

  1. Cái đầu tiên tạo một tham chiếu đến bộ sưu tập của
    Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    53. Hiện tại, chiều dài của nó là
    Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    54
  2. Các tập lệnh thứ hai chạy sau khi trình duyệt gặp một
    Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    53 nữa, vì vậy độ dài của nó là
    Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    56

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
1

Ngược lại,

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
57 trả về một bộ sưu tập tĩnh. Nó giống như một mảng các phần tử cố định

Nếu chúng ta sử dụng nó thay thế, thì cả hai tập lệnh đều xuất ra

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
54

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
2

Bây giờ chúng ta có thể dễ dàng nhận thấy sự khác biệt. Bộ sưu tập tĩnh không tăng sau khi xuất hiện một

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
59 mới trong tài liệu

Có 6 phương pháp chính để tìm kiếm các nút trong DOM

Phương thứcTìm kiếm theo. Có thể gọi một phần tử không?Live?
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
15CSS-selector✔-
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
57CSS-selector✔-________ 56
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
3--________ 474
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
71-✔________ 379tag hoặc
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
77✔✔
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
78class✔✔

Cho đến nay, được sử dụng nhiều nhất là

Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
15 và
Element
// elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
57, nhưng
let elem = 5; // now elem is 5, not a reference to
alert[elem]; // 5
61 có thể hữu ích hoặc được tìm thấy trong các tập lệnh cũ

bên cạnh đó

  • let elem = 5; // now elem is 5, not a reference to
    alert[elem]; // 5
    62 để kiểm tra xem
    Element
    // get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
    00 có khớp với bộ chọn CSS đã cho hay không
  • Element
    // elem is a reference to DOM-element with id="elem" elem.style.background = 'red'; // id="elem-content" has a hyphen inside, so it can't be a variable name // ...but we can access it using square brackets: window['elem-content']
    12 để tìm tổ tiên gần nhất khớp với bộ chọn CSS đã cho. Bản thân
    Element
    // get the element let elem = document.getElementById['elem']; // make its background red elem.style.background = 'red';
    00 cũng được kiểm tra

Và hãy đề cập đến một phương pháp khác ở đây để kiểm tra mối quan hệ cha-con, vì nó đôi khi hữu ích

Làm cách nào để lấy tất cả các phần tử div trong JavaScript?

getElementsByTagName[] sẽ chọn tất cả các phiên bản của một phần tử HTML nhất định trên trang web hiện tại dựa trên tên thẻ của nó, i. e. . getElementsByTagName["div"] là tất cả những gì bạn cần làm để chọn tất cả các phần tử

Làm cách nào để lấy dữ liệu div trong JavaScript?

Sử dụng thuộc tính textContent để lấy văn bản của phần tử div , e. g. kết quả const = phần tử. văn bảnnội dung. Thuộc tính textContent sẽ trả về nội dung văn bản của div và hậu duệ của nó. Nếu phần tử trống, một chuỗi rỗng được trả về.

Làm cách nào để chọn nhiều div trong JavaScript?

Sử dụng phương thức querySelectorAll[] để chọn các phần tử theo nhiều id , e. g. tài liệu. querySelectorAll['#box1, #box2, #box3']. Phương thức lấy một chuỗi chứa một hoặc nhiều bộ chọn làm tham số và trả về một tập hợp các phần tử phù hợp.

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

createDocumentFragment[]; . createElement['div']; . id = 'r'+i; . className = 'ansbox'; . appendChild[newDiv];

Chủ Đề