js如何写html页面

JavaScript010

js如何写html页面,第1张

js输出html中表格的方法如下:

document.write("<table border=1 >")

for(i=1i<=ri++)

{

document.write("<tr>")

for(j=1j<=cj++)

document.write("<td>"+Math.pow(j,i))  //输出数组

document.write("</tr>")

}

document.write("</table>")

运行结果:

所谓动态写入方法就是源文件代码中原来没有内容或者需要重新改变此处的要显示的文字或内容,需要用JavaScript代码来实现。动态写入是一种很常见常用的方法。

1、用innerHTML写入html代码:

<div id="abc"></div>

<script>document.getElementById("abc").innerHTML="要写入的文字或内容"</script>

2、appendChild() 方法:

<ul id="myList"><li>Coffee</li><li>Tea</li></ul>

<button onclick="myFunction()">点击向列表添加项目</button>

<script>

function myFunction(){

var node=document.createElement("LI")

var textnode=document.createTextNode("Water")

node.appendChild(textnode)

document.getElementById("myList").appendChild(node)

}

</script>