HTML DOM 允许 JavaScript 改变 HTML 元素的样式,设置text-align样式的代码为:
obj.style.textAlign = "left / right / center"实例演示如下:
1、HTML结构
<div id = "test">示例文字</div><input type="button" value="左对齐" onclick="fun1()">
<input type="button" value="居中" onclick="fun2()">
<input type="button" value="右对齐" onclick="fun3()">
2、javascript代码
function fun1(){document.getElementById("test").style.textAlign = "left"
}
function fun2(){
document.getElementById("test").style.textAlign = "center"
}
function fun3(){
document.getElementById("test").style.textAlign = "right"
}
3、效果演示