為什么該disabled屬性在 HTML 中應用于某個<style>部分時不起作用,但在通過 JS 應用時卻起作用。換句話說,如果我有:
<html>
<style id="myStyle" disabled>
body {
background-color: black;
color: white;
}
</style>
<script>
// document.getElementById("myStyle").disabled = true;
</script>
<body>
Hello world!
</body>
</html>
然后它將是黑色背景上的白色文本,即使該<style>部分具有該disabled屬性。但是如果我取消注釋 JS 行,樣式將被正確禁用,它將是白色背景上的黑色文本。我也嘗試disabled="true"了其他一些沒有效果的變體。
謝謝!
uj5u.com熱心網友回復:
根據 HTML 規范https://www.w3.org/TR/html401/present/styles.html#h-14.2.3,樣式標簽實際上沒有禁用屬性
<!ELEMENT STYLE - - %StyleSheet -- style info -->
<!ATTLIST STYLE
%i18n; -- lang, dir, for use with title --
type %ContentType; #REQUIRED -- content type of style language --
media %MediaDesc; #IMPLIED -- designed for use with these media --
title %Text; #IMPLIED -- advisory title --
>
但是,dom 規范確實有一個 disabled 屬性(https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-16428977):
interface HTMLStyleElement : HTMLElement {
attribute boolean disabled;
attribute DOMString media;
attribute DOMString type;
};
因此,您的 JS 可以更改 DOM 并禁用樣式標簽,但是,無法直接從 HTML 中禁用樣式標簽。
uj5u.com熱心網友回復:
根據規范,樣式沒有禁用屬性。
https://www.w3.org/TR/html401/present/styles.html#h-14.2.3
%i18n; -- lang, dir, 用于標題 -- type %ContentType; #REQUIRED -- 樣式語言的內容型別 -- 媒體 %MediaDesc; #IMPLIED -- 設計用于這些媒體 -- title %Text; #IMPLIED -- 咨詢標題 --
樣式也是一個頭標簽:
STYLE 元素允許作者將樣式表規則放在檔案的頭部。HTML 允許在檔案的 HEAD 部分中包含任意數量的 STYLE 元素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/405310.html
標籤:
