Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

คุณสมบัติ HTML DOM Input Week form

คุณสมบัติ form ของ HTML DOM Input Week คืนค่าการอ้างอิงถึงฟอร์มที่ครอบคลุมช่องป้อนสัปดาห์

ไวยากรณ์ (Syntax)

การดึงออบเจ็กต์ฟอร์ม:

inputWeekObject.form

ตัวอย่างการใช้งาน

ตัวอย่างเข้าถึง ID ฟอร์มผ่านช่องสัปดาห์ (ซ้ำกับตัวอย่างใน Input Week Object)

<!DOCTYPE html>
<html>
<head>
<title>Input Week form</title>
<style>
  form {
    width: 70%;
    margin: 0 auto;
    text-align: center;
  }
  * {
    padding: 2px;
    margin: 5px;
  }
  input[type="button"] {
    border-radius: 10px;
  }
</style>
</head>
<body>
<form id="Mid-Term">
  <fieldset>
    <legend>Week-form</legend>
    <label for="WeekSelect">สัปดาห์สอบ: <input type="week" id="WeekSelect" value="2019-W36"> </label> 
    <input type="button" onclick="showExamination()" value="What exams are in this week? "> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form> 
<script> 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputWeek = document.getElementById("WeekSelect"); 
  function showExamination() { 
    divDisplay.textContent = 'Examinations: ' + inputWeek.form.id; 
  } 
</script> 
</body>
</html>

ผลลัพธ์

ก่อนคลิกปุ่ม - ช่องสัปดาห์แสดง 2019-W36

คลิก "What exams are in this week?" - แสดง "Examinations: Mid-Term"

ประโยชน์: เข้าถึงฟอร์มพาเรนต์ได้โดยตรง ไม่ต้อง querySelector แยกต่างหาก