Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Đối tượng Input Week trong HTML DOM

Đối tượng Input Week trong HTML DOM đại diện cho phần tử <input type="week"> — cho phép người dùng chọn tuần và năm (theo chuẩn ISO 8601: YYYY-Www) mà không cần thư viện lịch phức tạp.

Tạo đối tượng Input Week

var weekObject = document.createElement("INPUT"); weekObject.type = "week";

Các thuộc tính chính

Thuộc tínhMô tả
autocompleteGiá trị autocomplete của ô week.
autofocusTự động focus khi tải trang.
defaultValueGiá trị mặc định ban đầu.
disabledVô hiệu hóa ô week.
formTham chiếu form chứa ô week.
maxTuần/năm lớn nhất cho phép.
minTuần/năm nhỏ nhất cho phép.
nameThuộc tính name của ô week.
readOnlyChỉ đọc hay không.
requiredBắt buộc chọn hay không.
stepBước nhảy tuần (mặc định 1).
typeLoại form element (luôn "week").
valueGiá trị hiện tại (YYYY-Www).

Các phương thức

Phương thứcMô tả
stepDown(n)Giảm n tuần (mặc định 1).
stepUp(n)Tăng n tuần (mặc định 1).

Ví dụ minh họa

Sử dụng thuộc tính form để lấy ID kỳ thi từ form cha:

<!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">Tuần thi: <input type="week" id="WeekSelect" value="2019-W36"> </label> 
    <input type="button" onclick="hienThiKyThi()" value="Kỳ thi nào tuần này?"> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form>
<script> 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputWeek = document.getElementById("WeekSelect"); 
  function hienThiKyThi() { 
    divDisplay.textContent = 'Kỳ thi: ' + inputWeek.form.id; 
  } 
</script> 
</body> 
</html>

Kết quả

  • Ban đầu: ô week 2019-W36.
  • Nhấn Kỳ thi nào tuần này?: "Kỳ thi: Mid-Term".

Lưu ý: type="week" không hỗ trợ trên Safari (tính đến 2024). Cần polyfill hoặc fallback cho production.