Style font 屬性
Style 對象
定義和用法
font 屬性以速記形式設置或返回最多六個獨立的字體屬性。
通過該屬性,您可以設置/返回:
- font-style
- font-variant
- font-weight
- font-size
- line-height
- font-family
上述的屬性,也可以設置單獨的樣式屬性。強烈建議使用單獨的屬性這樣有更好的可控性。
語法
設置 font 屬性:
Object.style.font="[style variant weight size/lineHeight family]
or [reserved word]"
返回 font 屬性:
Object.style.font
提示:font 屬性沒有默認值。
| 值 | 描述 |
|---|---|
| style | 設置字體樣式。 |
| variant | 設置文本以小型大寫字母字體顯示。 |
| weight | 設置字體粗細。 |
| size | 設置字體尺寸。 |
| lineHeight | 設置行與行之間的距離。 |
| family | 設置字體系列。 |
| caption | 為控件定義字體(比如按鈕、下拉列表等)。 |
| icon | 定義用於標注圖示的字體。 |
| menu | 定義菜單中使用的字體。 |
| message-box | 定義對話框中使用的字體。 |
| small-caption | 定義小型控件中使用的字體。 |
| status-bar | 定義窗口狀態欄中使用的字體。 |
| inherit | font 屬性的值從父元素繼承。 |
流覽器支持
![]()
![]()
![]()
![]()
![]()
所有主要流覽器都支持 font 屬性。
注意:IE7 及更早的版本不支持 "inherit" 值。IE8 只有規定了 !DOCTYPE 才支持 "inherit"。IE9 支持 "inherit"。
實例
實例
更改文本的字體:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
<script>
function displayResult(){
document.getElementById("p1").style.font="italic bold 20px arial,serif";
}
</script>
</head>
<body>
<p id="p1">這是一些文本。</p>
<br>
<button type="button" onclick="displayResult()">修改字體</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
<script>
function displayResult(){
document.getElementById("p1").style.font="italic bold 20px arial,serif";
}
</script>
</head>
<body>
<p id="p1">這是一些文本。</p>
<br>
<button type="button" onclick="displayResult()">修改字體</button>
</body>
</html>
Style 對象
