Categories
Computer Data

การเขียนโค้ดเพื่อใช้งาน GraphQL โดยไม่ใช้ไลบรารี

เวลาที่ส่งข้อมูลระหว่าง Client และ Server ผ่านทางจาวาสคริปโดยเรียกใช้งานผ่านทาง AJAX ปกติ เราจะส่งข้อมูลโดยวิธี POST ผ่านการพิมพ์โค้ดตามด้านล่างนี้ (ในตัวอย่างจะใช้ XMLHttpRequest แต่จะใช้ Fetch, Axios หรืออื่น ๆ ก็ได้ เพราะได้ผลเหมือนกัน)

Categories
Computer Diary

วิธีการเลือก parent element ด้วย JavaScript

ปกติเวลาที่เราเลือก element ด้วยภาษาจาวาสคริป เราจะใช้คำสั่งที่รู้จักกันอย่าง

document.getElementById("id");
document.document.getElementsByClassName("class name");
document.document.getElementsByName("name");
document.document.getElementsByTagName("tag name");

หรือเลือก element โดยการใช้ CSS Selector ด้วยคำสั่งที่รู้จักอย่าง

document.querySelector("selector");
parent_element.querySelector("selector");
document.querySelectorAll("selector");
parent_element.querySelectorAll("selector");

คำสั่งเหล่านี้ะคืนค่า element ที่เราเลือกไว้ ไม่ว่าจะเป็น element เดียว หรือ element ทั้งหมดที่เราพบในหน้าเว็บเพจนั้น ๆ อย่างไรก็ดีเราต้องการเลือก parent element จาก element ที่อยู่ภายใน จุดนี้เราจะทำได้อย่างไร?

Exit mobile version