分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

insertAdjacentHTML方法详解

发布时间:2023-09-06 02:28责任编辑:蔡小小关键词:HTML

添加HTML内容与文本内容以前用的是innerHTML与innerText方法,

最近发现还有insertAdjacentHTML和 insertAdjacentText方法,

这两个方法更灵活,可以在指定的地方插入html内容和文本内容。

insertAdjacentText方法与 insertAdjacentHTML方法类似,只不过只能插入纯文本,参数相同 
方法名称:insertHtml(where,el,html)

参数介绍:

  • where:插入位置。包括beforeBegin,beforeEnd,afterBegin,afterEnd
  • el:用于参照插入位置的html元素对象
  • html:要插入的html代码

insertAdjacentHTML 方法:

在指定的地方插入html标签语句 
原型:insertAdajcentHTML(swhere,stext)

参数:

swhere: 指定插入html标签语句的地方, 
stext:要插入的内容

有四种值可用:

  • beforeBegin: 插入到标签开始前
  • afterBegin:插入到标签开始标记之后
  • beforeEnd:插入到标签结束标记前
  • afterEnd:插入到标签结束标记后
<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> ?<html> ?????<head> ?????<title>24.htm insertAdjacentHTML插入新内容</title> ?????<script language="jscript"> ???????function addsome(){ ?????????????document.all.paral.insertAdjacentHTML("afterBegin","<h1> 在文本前容器内插入内容1</h1>"); ?????????????document.all.paral.insertAdjacentHTML("beforeEnd","<h2> 在文本后容器内插入内容2</h2>"); ?????????????document.all.paral.insertAdjacentHTML("beforeBegin","<h4> 在文本前容器外插入内容4</h1>"); ?????????????document.all.paral.insertAdjacentHTML("afterEnd","<h5> 在文本后容器外插入内容5</h2>"); ?????????} ?????</script> ?????</head> ?????<body onload="addsome()"> ?????<div id="paral" style="fontsize:6;color=‘#ff00ff‘" mce_style="fontsize:6;color=‘#ff00ff‘">原来的内容</div><hr> ?????</body> ?</html> 

  

insertAdjacentHTML方法详解

原文地址:https://www.cnblogs.com/wuchaofan1993/p/10207145.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved