ใช้ jQuery attr() ดึงค่า Attribute จากสตริง XML
ตัวอย่างโค้ด
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>
<script>
var yourXMLDetails = '<Game id="101" details="Play the game and win the amount">'
+ '<gameName name="Cricket" content="/localhost/content/game/play/" />'
+ '<gameName name="Tennis" content="/localhost/game/all/scratch/" />'
+ '</Game>';
console.log("Game Details===" + $(yourXMLDetails).attr("details"));
</script>
</body>
</html>
วิธีรันโปรแกรม
- บันทึกไฟล์ชื่อ "anyName.html" หรือ "index.html"
- คลิกขวาที่ไฟล์ เลือก "Open with Live Server" ใน VS Code Editor
ผลลัพธ์
แสดงค่า Attribute "details" ของ Element Game ใน Console