nút. Phương thức chứa [] được sử dụng để kiểm tra xem một nút đã cho có phải là hậu duệ của một nút khác ở bất kỳ cấp độ nào không. Hậu duệ có thể trực tiếp là cha mẹ của đứa trẻ hoặc xa hơn nữa trong chuỗi. Nó trả về một giá trị boolean của kết quả. Phương thức này được sử dụng trên phần tử cha và tham số được truyền trong phương thức là phần tử con cần kiểm tra. Nó trả về true nếu đứa trẻ là hậu duệ của cha mẹ. Điều này có nghĩa là phần tử là con của cha mẹ.
cú pháp.
function checkParent[parent, child] { if [parent.contains[child]] return true; return false; }
Thí dụ.
html
.parent,
.child,
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }0
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }1
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }2
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }1
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }4
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }1
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }6
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }8
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }9
style
>
2
1
>
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }9
>
1>
1
>
2
.parent,
7
function checkParent[parent, child] { let node = child.parentNode; // keep iterating unless null while [node != null] { if [node == parent] { return true; } node = node.parentNode; } return false; }9
>
1>
2
đầu ra
Làm cách nào để kiểm tra xem một phần tử có phải là con của cha mẹ hay không bằng JavaScript?
Phương pháp 2. Vòng qua cha mẹ của đứa trẻ đã cho
Phần tử con có thể được kiểm tra để có phần tử cha đã cho bằng cách lặp liên tục lần lượt qua phần tử cha của phần tử. Cha của mỗi nút được tìm thấy bằng cách truy cập thuộc tính parentNode trả về nút cha nếu có. Một vòng lặp while được sử dụng cho đến khi tìm thấy phần tử cha được yêu cầu hoặc không còn phần tử cha nào tồn tại. Bên trong vòng lặp này, nút cha của mỗi phần tử được tìm thấy trong mỗi lần lặp. Nếu nút cha khớp với nút đã cho trong bất kỳ lần lặp nào, điều đó có nghĩa là phần tử đó là con của nút cha.