如何用js动态写入html代码

html-css014

如何用js动态写入html代码,第1张

所谓动态写入方法就是源文件代码中原来没有内容或者需要重新改变此处的要显示的文字或内容,需要用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>

1、J首先输入s_file01.js,//程序代码 document.write(" <script language=\"javascript\" src="\/com\/ Js_file02.js \" ><\/script>")。

2、 html代码 代码如下:<html> <body> <input type="button" value="ok" onclick="javascript:fun_b()"> </body> //必须在这个位置,也即<body>...</body>之下: <script language="JAVASCRIPT" src='Js_b.js'></script> </html>。

3、Js_file01.js 代码如下://程序代码 document.scripts[0].src=" Js_file02.js"。

4、看到自己的JS文件。

5、可以看到js插入成功了。