轉自網絡
添加HTML內容與文本內容以前用的是innerHTML與innerText方法,最近發覺還有insertAdjacentHTML和insertAdjacentText要領,這兩個要領更靈活,可以在指定的地點插入html內容和文本內容。
insertAdjacentHTML要領:在指定的地點插入html標簽語句
原型:insertAdajcentHTML(swhere,stext)
參數:
swhere: 指定插入html標簽語句的地點,有四種值可用:
1. beforeBegin: 插入到標簽開始前
2. afterBegin:插入到標簽開始標記之后
3. beforeEnd:插入到標簽結束標記前
4. afterEnd:插入到標簽結束標記后
stext:要插入的內容
1
2 DEOM:
3
4 <html>
5
6 <head>
7
8 <script language="javascript">
9
10 function myfun()
11
12 {
13
14 var obj = document.getElementById("btn1");
15
16 obj.insertAdjacentHTML("afterEnd","<br><input name=\"txt1\">");
17
18 }
19
20 </script>
21
22 </head>
23
24 <body>
25
26 <input name="txt">
27
28 <input id="btn1" name="btn1" type="button" value="更多
" onclick="myfun()">
29
30 </body>
31
32 </html>