insertAdjacentHTML動態插入行

來源:互聯網
上載者:User
添加HTML內容與文本內容以前用的是innerHTML與innerText方法,最近發現還有insertAdjacentHTML和insertAdjacentText方法,這兩個方法更靈活,可以在指定的地方插入html內容和文本內容。

insertAdjacentHTML方法:在指定的地方插入html標籤語句

原型:insertAdajcentHTML(swhere,stext)

參數:

swhere: 指定插入html標籤語句的地方,有四種值可用:

1.     beforeBegin: 插入到標籤開始前

2.     afterBegin:插入到標籤開始標記之後

3.     beforeEnd:插入到標籤結束標記前

4.     afterEnd:插入到標籤結束標記後

stext:要插入的內容

<html>

   <head>

    <script language="javascript">

       function myfun()

         {

             var obj = document.getElementById("btn1");

              obj.insertAdjacentHTML("afterEnd","<br><input name=\"txt1\">");

         }

      </script>

   </head>

   <body>

     <input name="txt">

     <input id="btn1" name="btn1" type="button" value="更多..." onclick="myfun()">

   </body>

</html>

=============================

<head>
<title>24.htm insertAdjacentHTML插入新內容</title>
<script language="jscript">
function addsome()
{
document.all.paral.insertAdjacentHTML("afterBegin","<h1>在文本前容器插入入內容</h1>");
document.all.paral.insertAdjacentHTML("beforeEnd","<h2>在文本後容器插入入內容</h2>");
document.all.paral.insertAdjacentHTML("beforeBegin","<h4>在文本前容器外插入內容</h1>");
document.all.paral.insertAdjacentHTML("afterEnd","<h5>在文本後容器外插入內容</h2>");
}
</script>
</head>
<body onload="addsome()">
<div id="paral" style="fontsize:6;color='#ff00ff'">原來的內容</div><hr>
</body>
</html>

=================================

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD><BODY>
<script>
var num=0;
var No_sys=0;function Add_button(){
if(No_sys<8){
   c_input.insertAdjacentHTML("beforeEnd","<div id=\"bar"+num+"\" oncontextmenu=\"Remove_button(bar"+num+");return   false\" style=\"background:red;width:40;height:20\">"+num+"</div>");
   num++;
   No_sys++;
}
}function Remove_button(obj){
obj.removeNode(true);
No_sys--;
}
</script>
<input type="button" onclick="Add_button()" value="動態加">
<input type="button" onclick="alert(c_input.innerHTML)" value="看">
<div id="c_input">
</div>  
</BODY>
</HTML>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.