- 1、如何用js動態寫入html代碼
- 2、如何使用js動態生成html代碼
- 3、js中寫的html代碼怎麼運行
所謂動態寫入方法就是源文件代碼中原來沒有內容或者需要重新改變此處的要顯示的文字或內容,需要用JavaScript代碼來實現。動態寫入是一種很常見常用的方法。
1、用innerHTML寫入html代碼:
div id=”abc”/div
scriptdocument.getElementById(“abc”).innerHTML=”要寫入的文字或內容”/script
2、appendChild() 方法:
ul id=”myList”liCoffee/liliTea/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
所謂動態生成html代碼就是源文件代碼中原來沒有內容或者需要重新改變此處的要顯示的文字或內容,需要用JavaScript代碼來實現。動態生成是一種很常見常用的方法。
用innerHTML寫入html代碼:
div id=”zd”/div
『即為向id為zd的標籤寫入內容
scriptdocument.getElementById(“zd”).innerHTML=”這裡即為要寫入的代碼”/script
說明:
innerHTML:向對象插入內容。
JS輸入輸出HTML代碼有2種方式:
1、在需要輸出的的位置寫JS代碼:scriptdocument.write(‘需要輸出的內容’)/script
比如:
ul
scriptdocument.write(‘lia href=”/wap2/newsPage/1320″5/a/lilia href=”/wap2/newsPage/1319″4/a/lilia href=”/wap2/newsPage/1318″3/a/lilia href=”/wap2/newsPage/1317″2/a/lilia href=”/wap2/newsPage/1316″1/a/li’);
/script
/ul
2、採用js的innerHTML方法:
例子;
ul id=”ul”/ul
script
document.getElementById(“ul”).innerHTML=’lia href=”/wap2/newsPage/1320″5/a/lilia href=”/wap2/newsPage/1319″4/a/lilia href=”/wap2/newsPage/1318″3/a/lilia href=”/wap2/newsPage/1317″2/a/lilia href=”/wap2/newsPage/1316″1/a/li’;
/script
原創文章,作者:Z5TYK,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126393.html