Js根据class名替换html标签内容 ,在某标签前后添加内容
function getClass(obj, attr) {
var aArray = [];//定义一个新的空数组
var i = 0;
var aAll = document.getElementsByTagName('*');//获取obj对象下面所有的节点
for (i = 0; i < aAll.length; i++) {
if (aAll[i].className == attr) {//判断当前对象的class名称是不是符合传进来的参数
aArray.push(aAll[i]);//如果符合则push到到aArray数组里边
alert(aAll[i]);
}
}
return aArray;//最后return一下
}
function insertHtml(where, el, html) {
where = where.toLowerCase();
if (el.insertAdjacentHTML) {
switch (where) {
case "beforebegin":
el.insertAdjacentHTML('BeforeBegin', html);
return el.previousSibling;
case "afterbegin":
el.insertAdjacentHTML('AfterBegin', html);
return el.firstChild;
case "beforeend":
el.insertAdjacentHTML('BeforeEnd', html);
return el.lastChild;
case "afterend":
el.insertAdjacentHTML('AfterEnd', html);
return el.nextSibling;
}
throw 'Illegal insertion point -> "' + where + '"';
}
var range = el.ownerDocument.createRange();
var frag;
switch (where) {
case "beforebegin":
range.setStartBefore(el);
frag = range.createContextualFragment(html);
el.parentNode.insertBefore(frag, el);
return el.previousSibling;
case "afterbegin":
if (el.firstChild) {
range.setStartBefore(el.firstChild);
frag = range.createContextualFragment(html);
el.insertBefore(frag, el.firstChild);
return el.firstChild;
} else {
el.innerHTML = html;
return el.firstChild;
}
case "beforeend":
if (el.lastChild) {
range.setStartAfter(el.lastChild);
frag = range.createContextualFragment(html);
el.appendChild(frag);
return el.lastChild;
} else {
el.innerHTML = html;
return el.lastChild;
}
case "afterend":
range.setStartAfter(el);
frag = range.createContextualFragment(html);
el.parentNode.insertBefore(frag, el.nextSibling);
return el.nextSibling;
}
throw 'Illegal insertion point -> "' + where + '"';
}
//调用1
getClass("div","banner")[0].innerHTML='我们来了';
//调用2
//insertHtml('afterBegin', document.getElementById("csContentEnd"), "<dd>在文本前容器内插入内容</dd>");
//insertHtml('beforeEnd', elem, "<dd>在文本后容器内插入内容</dd>");
insertHtml('beforeBegin', document.getElementById("csContentEnd"), "<dd>在文本前容器外插入内容</dd>");
//insertHtml('afterEnd', elem, "<dd>在文本后容器外插入内容</dd>");
Js根据class名替换html标签内容 ,在某标签前后添加内容的更多相关文章
- notepad++每行首尾添加内容
有时候我们需要给一个文本文件的每行前面或后面添加一些内容,例如我们一个文本文件里存放了很多图片的地址,现在我们需要把这些图片批量转换成html标记 百度经验:jingyan.baidu.com 工具/ ...
- 通过js去掉所有的html标签,得到HTML标签中的所有内容
<script> //替换掉所有的 html标签,得到Html标签中的内容 var s="<P><FONT face=宋体 color=#000000> ...
- js清除节点内容(改变标签元素)
<!DOCTYPE HTML><html> <head> <meta http-equiv="Content-Type" c ...
- 通过js给某个标签添加内容或者删除标签
添加内容 //先保存div中原来的html var tag = document.getElementById("tag").innerHTML; //构造新的内容 var cou ...
- 如何用PC标签在列表页中调出文章内容 phpcms
如何用PC标签在列表页中调出文章内容 phpcms v9 moreinfo=”"参数说明 {pc:content action="lists" catid="$ ...
- textarea与标签组合,点击标签填入标签内容,再次点击删除内容(vue)
需求:将textarea与span标签组合,点击标签自动填入标签文本内容,再次点击删除标签文本对应内容 原理:点击标签时,将标签内容作为参数,将内容拼接在textarea的value后面,再次点击标签 ...
- 内容模块PC标签调用说明
内容模块 模块名:content 模块提供的可用操作 操作名 说明 lists 内容数据列表 relation 内容相关文章 hits 内容数据点击排行榜 category 内容栏目列表 positi ...
- 原创:js代码, 让dedecms支持Tag选择, 添加内容更为方便,不用手输Tag
dedecms在编辑修改内容时,TAG标签需要手动输,中文的Tag, 中间还得用半角字符','分隔, 输入法切来切去很不方便, 于是动手改后台代码, 利用后台的tags_main.php, 让d ...
- phpcms V9首页、列表页以及内容页调用标签
1.页面标题:{$title} 2.发表时间:{$inputtime} 3.内容来源:{$copyfrom} 4.文章内容:{$content} 5.缩略图地址:{$thumb} 6.组图列表: {l ...
随机推荐
- 【定位:PDF文件定位关键字所在坐标和页码】
iText简介: iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件 ...
- 真正实现Netty私有协议开发
首先<Netty权威指南>私有协议开发那一章的样例代码是编译不通过的(但是这丝毫不影响本书的价值)处理方案可以参考:http://www.itnose.net/detail/6112870 ...
- 同一级目录js的引入
- linux deepin 12.12.1和Windows 7双系统修改默认启动项
1.打开终端,输入 sudo gedit /etc/default/grub ,然后输入密码.2.在出来的文本编辑器中找到 GRUB_DEFAULT=0 一行,把0改为2(因为我的Windows 7在 ...
- 安卓请求服务器js文件下载到本地,版本号就下载
<?phpreturn array('CJ_V' => 'v0.15',) <script src="/js/reserve.js?<?=C('CJ_V')?> ...
- Android ListView 无法响应onItemClick事件
当在布局文件中加入了Button.ImageButton.CheckBox.RadioButton等控件(也就是Button或者Checkable的子类控件)的时候,listView是无法响应onIt ...
- 【转】Storm并行度详解
1.Storm并行度相关的概念 Storm集群有很多节点,按照类型分为nimbus(主节点).supervisor(从节点),在conf/storm.yaml中配置了一个supervisor,有多个槽 ...
- Spring AOP实现声明式事务代码分析
众所周知,Spring的声明式事务是利用AOP手段实现的,所谓"深入一点,你会更快乐",本文试图给出相关代码分析. AOP联盟为增强定义了org.aopalliance.aop.A ...
- Python & virtualenv使用说明
virtualenv是virtual environment的缩写,可以创建独立的Python环境,用起来比较干净: 安装(已安装pip 或者 easy_install): 如果使用pip: pi ...
- TortoiseGit - 分支管理 -增加分支
1.本地当前在master分支,右击选择Create Branch... 2.填写Branch的名称 3.右击选择Push,在Ref-Local,选择刚才新建的Branch名称 4.点击OK后,在弹出 ...