TAILIEUCHUNG - Simply JavaScript phần 8

Một yếu tố chỉ có thể có một phụ huynh, nhưng nó có thể có nhiều trẻ em, vì vậy childNodes thực sự là một danh sách các nút có chứa tất cả các trẻ em của phần tử, để nguồn. Một khi chúng ta có một tham chiếu đến các yếu tố neo, chúng ta có thể nhận được một tham chiếu đến đoạn cha mẹ của nó bằng cách sử dụng parentNode như vậy | 80 Simply JavaScript Finding a Parent Every element node except for the document node has a parent. Consequently each element node has a property called parentNode. When we use this property we receive a reference to the target element s parent. Consider this HTML p a id oliver href oliver Oliver Twist a p Once we have a reference to the anchor element we can get a reference to its parent paragraph using parentNode like so var oliver oliver var paragraph Finding Children The parent-child relationship isn t just one way. You can find all of the children of an element using the childNodes property. An element can only have one parent but it can have many children so childNodes is actually a node list that contains all of the element s children in source order. Take for instance a list like this ul id baldwins li Alec li li Daniel li li William li li Simply JavaScript Document Access 81 Stephen li ul The unordered list node will have four child nodes 1 each of which matches a list item. To get the third list item the one containing William in the list above we d get the third element in the childNodes list var baldwins baldwins var william 2 Two shortcut properties are available to help us get the first child or last child of an element the firstChild and lastChild properties respectively. To get the Alec list item we could just use var alec And to get the Stephen list item we can use var stephen I don t think firstChild is all that much easier than typing childNodes 0 but lastChild is definitely shorter than childNodes - 1 so it s a shortcut that I use regularly. Finding Siblings As well as moving up and down the DOM tree we can move from side to side by getting the next or previous node on the same level. The properties we use to do so are nextSibling and previousSibling. If we continued on from the example

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.