HTML DOM 輸入月 max 屬性


HTML DOM 輸入月 max 屬性返回並修改 HTML 文件中 type=”month”的輸入欄位 max 屬性的值。

語法

語法如下 −

1. 返回 max

object.max

2. 修改 max

object.max = “YYYY-MM”

此處,YYYY 表示年份,MM 表示月份,例如“2019-02”

例項

讓我們看一個 HTML DOM 輸入月 max 屬性的例項 −

 實際示例

<!DOCTYPE html>
<html>
<head>
<style>
   html{
      height:100%;
   }
   body{
      text-align:center;
      color:#fff;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%)
      center/cover no-repeat;
      height:100%;
   }
   p{
      font-weight:700;
      font-size:1.1rem;
   }
   input{
      display:block;
      width:35%;
      border:2px solid #fff;
      background-color:transparent;
      color:#fff;
      font-weight:bold;
      padding:8px;
      margin:1rem auto;
   }
   .btn{
      background:#0197F6;
      border:none;
      height:2rem;
      border-radius:2px;
      width:35%;
      margin:2rem auto;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
   .show{
      font-size:1.5rem;
      font-weight:bold;
   }
</style>
</head>
<body>
<h1>DOM Input month max/min property Demo</h1>
<p>Hi, Do you need leave from your work?</p>
<p>If yes then select your desired one month between April & August:</p>
<input type="month" class="monthInput" min="2019-04" max="2019-08">
<button onclick="showMySelection()" class="btn">Show Result</button>
<div class="show"></div>
<script>
   function showMySelection() {
      var monthInput = document.querySelector(".monthInput");
      var showMsg = document.querySelector(".show");
      if(monthInput.value === ''){
         showMsg.innerHTML="Please Select!!";
      } else {
         showMsg.innerHTML=monthInput.value;
      }
   }
</script>
</body>
</html>

輸出

將生成以下輸出 −

使用月份輸入欄位選擇任何月份(4 月至 8 月之間),然後點選“ 顯示結果顯示結果”按鈕,顯示所選月 −

點選“顯示結果”按鈕 −

更新於:2020 年 6 月 13 日

100 次瀏覽

開啟你的 職業

完成課程後獲得認證

開始
廣告
© . All rights reserved.