div 中动态插⼊javascript 代码
<div id="test"></div>
<button οnclick="insertHTML()"/>
<script>
function insertHTML(){
var str="aaaaa"+"<script defer> alert('终于能运⾏JS 了,靠!'); </script" + ">";
}
</script>
添加HTML内容与⽂本内容以前⽤的是innerHTML与innerText⽅法,最近发现还有insertAdjacentHTML和insertAdjacentText⽅法,这两个⽅法更灵活,可以在指定的地⽅插⼊html内容和⽂本内容。
insertAdjacentHTML⽅法:在指定的地⽅插⼊html标签语句原型:insertAdajcentHTML(swhere,stext)
参数:
swhere: 指定插⼊html标签语句的地⽅,有四种值可⽤:
1. beforeBegin: 插⼊到标签开始前
2. afterBegin:插⼊到标签开始标记之后
3. beforeEnd:插⼊到标签结束标记前
4. afterEnd:插⼊到标签结束标记后
stext:要插⼊的内容
1 < html >
2 < head >
3 < script language ="javascript" >
4 function myfun(){
5 var obj = ElementById( " btn1 " );
6 obj.insertAdjacentHTML( " afterEnd " , " <br><input name= " txt1 " > " );
7 }
8 </ script >
9 </ head >
10 < body >
11 < input name ="txt" >
Html代码
1. <div id ="test"></div>
2. <button onclick ="insertHTML()"/>
3.
4. <script>
5. function insertHTML(){
6. var str ="aaaaa"+"<script defer > alert('终于能运⾏JS了,靠!'); </script " + ">";
7. ElementById("test").insertAdjacentHTML("afterBegin", str);
8. }
9. </script>
12<input id="btn1" name="btn1" type="button" value="更多" onclick="myfun()">
13</body>
14</html>
************************************************************************************
1<html>
2<head>
3<title>24.htm insertAdjacentHTML插⼊新内容</title>
4<script language="jscript">
5function addsome()
6{
7 document.all.paral.insertAdjacentHTML("afterBegin","<h1>在⽂本前容器内插⼊内容</h1>");
8 document.all.paral.insertAdjacentHTML("beforeEnd","<h2>在⽂本后容器内插⼊内容</h2>");
9 document.all.paral.insertAdjacentHTML("beforeBegin","<h4>在⽂本前容器外插⼊内容</h1>");
10 document.all.paral.insertAdjacentHTML("afterEnd","<h5>在⽂本后容器外插⼊内容</h2>");
11}
12</script>
13</head>
14<body onload="addsome()">
15<div id="paral" >原来的内容</div><hr>
16</body>
javascript动态效果17</html>
**********************************************************************************
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<HTML>
3<HEAD>
4<TITLE> New Document </TITLE>
5<META NAME="Generator" CONTENT="EditPlus">
6<META NAME="Author" CONTENT="">
7<META NAME="Keywords" CONTENT="">
8<META NAME="Description" CONTENT="">
9</HEAD>
10<BODY>
11<script>
12var num=0;
13var No_sys=0;
14function Add_button(){
15if(No_sys<8){
16 c_input.insertAdjacentHTML("beforeEnd","<div id="bar"+num+"" οncοntextmenu="Remove_button(bar"+num+");return false" >"+num+"</div>");
17 num++;
18 No_sys++;
19}
20}
21function Remove_button(obj){
23No_sys--;
24}
25</script>
26<input type="button" onclick="Add_button()" value="动态加">
27<input type="button" onclick="alert(c_input.innerHTML)" value="看">
28<div id="c_input">
29</div>
30</BODY>
31</HTML>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论