insertAdjacentHTML方法示例
2010-01-08 11:03
28 查看
添加HTML内容与文本内容以前用的是innerHTML与innerText方法,最近发现还有insertAdjacentHTML和insertAdjacentText方法,这两个方法更灵活,可以在指定的地方插入html内容和文本内容。
insertAdjacentHTML方法:在指定的地方插入html标签语句
原型:insertAdajcentHTML(swhere,stext)
参数:
swhere: 指定插入html标签语句的地方,有四种值可用:
1. beforeBegin: 插入到标签开始前
2. afterBegin:插入到标签开始标记之后
3. beforeEnd:插入到标签结束标记前
4. afterEnd:插入到标签结束标记后
stext:要插入的内容
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="more" onclick="myfun()">
</body>
</html>
<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>
<html>
<script>
var a = 0;
function Remove_button(obj){
obj.removeNode(true);
}
function Add_button(){
c_input.insertAdjacentHTML("beforeEnd", "<div id=bar" + a +
" style='background:red;width:40;height:20'" +
"oncontextmenu='Remove_button(bar" +
a +
"); return false'>" +
a +
"sss</div>")
a++;
}
</script>
<input type="button" onclick="Add_button()" value="add"><input type="button" onclick="alert(c_input.innerHTML)" value="look">
<div id="c_input">
</div>
</html>
相关文章推荐
- insertAdjacentHTML方法示例
- HTML标签中通过字符串插入HTML-- insertAdjacentHTML方法示例
- insertAdjacentHTML方法示例
- insertAdjacentHTML方法示例
- insertAdjacentHTML方法示例
- insertAdjacentHTML方法示例
- insertAdjacentHTML方法示例
- insertAdjacentHTML方法示例
- insertAdjacentHTML方法示例
- insertAdjacentHTML方法示例
- insertAdjacentHTML方法详解
- 动态添加内容 insertAdjacentText()方法和insertAdjacentHTML方法
- insertAdjacentHTML方法详解
- insertAdjacentHTML方法详解
- insertAdjacentHTML和insertAdjacentText方法(转)
- insertAdjacentHTML方法详解
- 修改html很实用的insertAdjacentHTML方法
- 【javascript基础】insertAdjacentHTML和insertAdjacentText方法
- insertAdjacentHTML和insertAdjacentText方法
- EXT2中一段兼容FF的insertAdjacentHTML方法