<!doctype html public " - // w3c // dtd xhtml 1.1 // en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd"> <html xmlns = " xml:lang = "en"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title>テスト</ittite> </head> <script = "text/javascript" src = "src =" example.js "> </<body> <div id = "testdiv"> </div> </body> </html>
example.js:
window.onload = function(){var testdiv = document.getElementById( "testdiv"); testdiv.innerhtml = "<p>この<em>この</em>コンテンツを挿入しました。</p>";}另一段代码:
window.onload = function(){var para = document.createelement( "p"); var txt1 = document.createTextNode( "I挿入"); var emphasis = document.createelement( "em"); var txt2 = document.createTextNode( "this"); var txt3 = document.createTextNode( "content。"); Para.AppendChild(TXT1); Emphasis.AppendChild(TXT2); para.appendChild(emphasis); Para.AppendChild(TXT3); var testdiv = document.getElementById( "testdiv"); testdiv.appendChild(para);}这与在div内动态载入另一个页面非常相似!
以上这篇js把内容动态插入到div、希望能给大家一个参考、也希望大家多多支持武林网。