樣式沒有 “required” 屬性的元素和 CSS


使用 CSS 中的 :optional 選擇器,為沒有 “required” 屬性的 <input> 元素設定樣式。

示例

可以嘗試執行下列程式碼,以實現 :optional 選擇器

線上演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         input:optional {
            background-color: blue;
         }
      </style>
   </head>
   <body>
      <form>
         Subject: <input type = "text" name = "subject"><br>
         Student: <input type = "text" name = "student" required><br>
      </form>
   </body>
</html>

更新日期:2020 年 7 月 1 日

202 次瀏覽

開啟你的 職業

完成課程,獲得認證

立即開始
廣告