使用document对象改变HTML输出流
JavaScript中的document对象能够动态的创建HMTL内容。document.write()方法可用于直接向HTML的输出流写内容。
<body>
<script type="text/javascript">
var time = new Date();
var day = time.Date();
document.write("今天是当前月的第" + day + "天");
</script>
</body>
tips:
document.write()方法会在文档加载完成之后覆盖文档中的所有内容。
使用document对象改变HTML内容
修改HTML内容最简单的方法是使用innerHTML属性。
//语法结构:
//object.innerHTML(document.getElementById("").innerHTML)
<body>
<div id="box"></div>
<input id="click" type="button" value="添加文本"/>
<script type="text/javascript">
function insert(){
document.getElementById("box").innerHTML = "hello world";
}
document.getElementById("click").onclick = insert;
</script>
</body>
使用document.getElementById(“id”).style.样式属性 = 样式;
<body>
<div id="box"></div>
<input id="content" type="button" value="添加内容"/>
<input id="style" type="button" value="添加样式"/>
<script type="text/javascript">
function content(){
document.getElementById("box").innerHTML = "hello world";
}
function styles(){
var obj = document.getElementById("box")
obj.style.borderWidth = "1px";
obj.style.borderColor = "orange";
obj.style.borderStyle = "solid";
obj.style.color = "red";
}
document.getElementById("content").onclick = content;
document.getElementById("style").onclick = styles;
</script>
</body>
使用document对象改变HTML属性
//document.getElementById("id").属性 = 属性;