HTML DOM Style display 屬性
定義和用法
display 屬性設置或返回元素的顯示類型。
HTML 中的元素大多是"內聯"或"塊"元素:一個內聯元素,在其左側和右側都是浮動內容。 一個塊元素填滿整個行,並沒有什麼可顯示在其左側或右側。
display 屬性還允許作者顯示或隱藏一個元素。 與visibility 屬性類似。 然而,如果您設置display:none,將隱藏整個元素,如果您設置visibility:hidden,元素的內容將不可見,但元素保持原來的位置和大小。
語法
設置display 屬性:
Object.style.display="value"
返回display 屬性:
Object.style.display
值 | 描述 |
---|---|
block | 元素呈现为块级元素。 |
compact | 元素呈现为块级元素或内联元素,取决于上下文。 |
inherit | display 属性的值从父元素继承。 |
inline | 默认。元素呈现为内联元素。 |
inline-block | 元素呈现为内联盒子内的块盒子。 |
inline-table | 元素呈现为内联表格(类似 <table>),表格前后没有换行符。 |
list-item | 元素呈现为列表。 |
marker | 该值在盒子前后设置内容作为标记(与 :before 和 :after 伪元素一起使用,否则该值与 "inline" 是相同的)。 |
none | 元素不会被显示。 |
run-in | 元素呈现为块级或内联元素,取决于上下文。 |
table | 元素呈现为块级表格(类似 <table>),表格前后带有换行符。 |
table-caption | 元素呈现为表格标题(类似 <caption>)。 |
table-cell | 元素呈现为表格单元格(类似 <td> 和 <th>)。 |
table-column | 元素呈现为单元格的列(类似 <col>)。 |
table-column-group | 元素呈现为一个或多个列的分组(类似 <colgroup>)。 |
table-footer-group | 元素呈现为表格页脚行(类似 <tfoot>)。 |
table-header-group | 元素呈现为表格页眉行(类似 <thead>)。 |
table-row | 元素呈现为表格行(类似 <tr>)。 |
table-row-group | 元素呈现为一个或多个行的分组(类似 <tbody>)。 |
瀏覽器支持
所有主要瀏覽器都支持display 屬性。
注意: IE7及更早的版本不支持"inherit"值。IE8 只有規定了!DOCTYPE 才支持"inherit"。 IE9 支持"inherit"。
提示和註釋
提示:如果元素時塊元素,它的顯示類型可通過float屬性改變。
實例
實例
當點擊按鈕時設置元素不可見:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function demoDisplay(){
document.getElementById("p1").style.display="none";
}
function demoVisibility(){
document.getElementById("p2").style.visibility="hidden";
}
</script>
</head>
<body>
<p id="p1">这是一些文本。</p>
<p id="p2">这是一些文本。</p>
<input type="button" onclick="demoDisplay()" value="隐藏显示属性的文本">
<input type="button" onclick="demoVisibility()" value="具有可见性属性的隐藏文本">
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function demoDisplay(){
document.getElementById("p1").style.display="none";
}
function demoVisibility(){
document.getElementById("p2").style.visibility="hidden";
}
</script>
</head>
<body>
<p id="p1">这是一些文本。</p>
<p id="p2">这是一些文本。</p>
<input type="button" onclick="demoDisplay()" value="隐藏显示属性的文本">
<input type="button" onclick="demoVisibility()" value="具有可见性属性的隐藏文本">
</body>
</html>
嘗試一下»
Style對象