HTML DOM Style textAlign 屬性
定義和用法
textAlign 屬性設置或返回塊級元素中文本的水平對齊方式。
語法
設置textAlign 屬性:
Object.style.textAlign="left|right|center|justify|inherit"
返回textAlign 屬性:
Object.style.textAlign
值 | 描述 |
---|---|
left | 默认。把文本排列到左边。 |
right | 把文本排列到右边。 |
center | 把文本排列到中间。 |
justify | 根据 textJustify 属性对齐文本。 |
inherit | textAlign 属性的值从父元素继承。 |
瀏覽器支持
所有主要瀏覽器都支持textAlign 屬性。
注意: IE7及更早的版本不支持"inherit"值。IE8 只有規定了!DOCTYPE 才支持"inherit"。 IE9 支持"inherit"。
實例
實例
居中對齊p 元素中的文本:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function displayResult(){
document.getElementById("p1").style.textAlign="center";
}
</script>
</head>
<body>
<p id="p1">这是一些文本。</p>
<br>
<button type="button" onclick="displayResult()">对齐文本</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function displayResult(){
document.getElementById("p1").style.textAlign="center";
}
</script>
</head>
<body>
<p id="p1">这是一些文本。</p>
<br>
<button type="button" onclick="displayResult()">对齐文本</button>
</body>
</html>
嘗試一下»
Style對象