Cara menggunakan previouselementsibling javascript
Dapatkan konten HTML dari saudara sebelumnya item daftar: var x = document.getElementById("item2").previousElementSibling.innerHTML; Hasil x akan: Coffee (first li) Cobalah sendiri
" Properti previousElementSibling mengembalikan elemen sebelumnya dari elemen tertentu, di tingkat pohon yang sama. Perbedaan antara properti ini dan previousSibling , adalah bahwa previousSibling mengembalikan simpul saudara sebelumnya sebagai simpul elemen, node teks atau komentar node, sedangkan previousElementSibling mengembalikan saudara
simpul sebelumnya sebagai simpul elemen (ignores text and comment nodes) . Properti ini adalah read-only. Tip: Gunakan nextElementSibling properti untuk kembali elemen berikutnya dari elemen tertentu. Tip: Gunakan anak properti untuk kembali
setiap elemen anak dari elemen tertentu. node .previousElementSibling View Discussion Improve Article Save Article View Discussion Improve Article Save Article The previousElementSibling property in HTML DOM is used to return the previous element of the same level of the given element. If
no previous element found on the same level then it returns null. It is a read-only property. It is similar to previousSibling property but the difference is previousSibling property returns the previous sibling node as an element node, a text node or a comment node, while previousElementSibling property returns the previous sibling as an element node. Syntax: Return value: This property returns a previous sibling of the specified element or
null if the current element has no previous sibling. Example:
Output: After click on the button: Note: Don’t use white space between two sibling elements, otherwise, the result will be “undefined”. Supported Browsers: The browser supported by DOM previousElementSibling property are listed below:
|