HTML DOM 輸入月份 readOnly 屬性


HTML DOM 輸入月份 readOnly 屬性返回和修改 HTML 文件中輸入月份欄位是否為只讀。

語法

語法如下 −

1. 返回 readOnly

object.readOnly

2. 修改 readOnly

object.readOnly = true | false

示例

 線上演示

我們來看一下 HTML DOM 輸入月份 readOnly 屬性的示例 −

<!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;
   }
</style>
</head>
<body>
<h1>DOM Input month readOnly property Demo</h1>
<p>Hi, Select your month of birth?</p>
<input type="month" class="monthInput">
<button onclick="rFunction()" class="btn">Read Only</button>
<button onclick="rWFunction()" class="btn">Read & Write</button>
<script>
   function rFunction() {
      var monthInput = document.querySelector(".monthInput");
      monthInput.readOnly =true;
   }
   function rWFunction(){
      var monthInput = document.querySelector(".monthInput");
      monthInput.readOnly =false;
   }
</script>
</body>
</html>

輸出

這將產生以下輸出 −

現在,使用月份輸入選擇任何月份,然後點選“只讀”按鈕並嘗試更改你的選擇。它不允許你更改 −

現在,點選“可讀寫”並嘗試更改選擇。它允許你更改 −

更新於: 30-Jul-2019

82 次瀏覽

開啟您的 職業生涯

完成課程後獲取認證

開始
廣告
© . All rights reserved.