若要使用Javascript 動態設定HTML5 必要屬性,請依照下列步驟操作:
嘗試使用建議的W3C 語法來設定必要的屬性:
document.getElementById("edName").attributes["required"] = "";
doesn' t 觸發驗證檢查。
設定 HTML5 驗證布林屬性的正確方法是使用 element.required 屬性。
例如:
document.getElementById("edName").required = true;
其中 edName 是輸入元素的 ID。
在HTML5 中,布林屬性可以透過以下方式定義:
但是,當在標記中定義required 屬性時,該屬性的值不是以下選項中的任何一個:
edName.attributes.required = [object Attr]
這是因為required 是反射屬性,類似於id、name 和type。
反射屬性是存在於元素物件上的屬性本身。設定反射屬性的值會更新 HTML 中的對應屬性。
因此,以下兩種方法是等效的:
使用setter 屬性:
element.required = true;
使用setAttribute:
element.setAttribute("required", "");
清除反射屬性,使用removeAttribute:
element.removeAttribute("required");
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3