HTML DOM Style fontWeight 属性
htmlweb developmentfront end technology更新于 2024/3/15 1:16:17
HTML DOM fontWeight 属性用于设置或返回元素文本字符的粗细。
以下是语法 −
设置 fontWeight 属性 −
object.style.fontWeight = "normal|lighter|bold|bolder|value|initial|inherit"
以上属性解释如下
| 值 | 描述 |
|---|---|
| normal | 这是默认设置,不会对字体进行任何更改。 |
| lighter | 字体更浅 |
| bold | 将字体设置为粗体,比lighter字体更粗。 |
| bold | 将字体设置为比粗体更粗的粗体。 |
| 100 200 300 400 500 600 700 800 900 | 给出从细到粗字符的值范围。 Normal=400,700=Bold。 |
| initial | 将此属性设置为初始值。 |
| inherit | 继承父级属性值 |
让我们看一个 fontWeight 属性的示例 −
示例
<!DOCTYPE html>
<html>
<head>
<style>
#demo2,#demo1 {
font-family: 'times new roman';
font-size: 25px;
}
</style>
<script>
function changeFontWeight() {
document.getElementById("demo1").style.fontWeight="bold";
document.getElementById("demo2").style.fontWeight="bold";
document.getElementById("Sample").innerHTML="The font weight has been changed for the above paragraphs";
}
</script>
</head>
<body>
<div id="demo1" >This is demo text</div>
<div id="demo2">This is demo text</div>
<p>Change the font weight for the text inside above divs by clicking the below button</p>
<button onclick="changeFontWeight()">Change font Weight </button>
<p id="Sample"></p>
</body>
</html>
输出

点击"更改字体粗细"按钮 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

