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标签内容 ,在某标签前后添加内容的更多相关文章

  1. notepad++每行首尾添加内容

    有时候我们需要给一个文本文件的每行前面或后面添加一些内容,例如我们一个文本文件里存放了很多图片的地址,现在我们需要把这些图片批量转换成html标记 百度经验:jingyan.baidu.com 工具/ ...

  2. 通过js去掉所有的html标签,得到HTML标签中的所有内容

    <script> //替换掉所有的 html标签,得到Html标签中的内容 var s="<P><FONT face=宋体 color=#000000> ...

  3. js清除节点内容(改变标签元素)

    <!DOCTYPE HTML><html> <head>        <meta http-equiv="Content-Type" c ...

  4. 通过js给某个标签添加内容或者删除标签

    添加内容 //先保存div中原来的html var tag = document.getElementById("tag").innerHTML; //构造新的内容 var cou ...

  5. 如何用PC标签在列表页中调出文章内容 phpcms

    如何用PC标签在列表页中调出文章内容 phpcms v9 moreinfo=”"参数说明 {pc:content action="lists" catid="$ ...

  6. textarea与标签组合,点击标签填入标签内容,再次点击删除内容(vue)

    需求:将textarea与span标签组合,点击标签自动填入标签文本内容,再次点击删除标签文本对应内容 原理:点击标签时,将标签内容作为参数,将内容拼接在textarea的value后面,再次点击标签 ...

  7. 内容模块PC标签调用说明

    内容模块 模块名:content 模块提供的可用操作 操作名 说明 lists 内容数据列表 relation 内容相关文章 hits 内容数据点击排行榜 category 内容栏目列表 positi ...

  8. 原创:js代码, 让dedecms支持Tag选择, 添加内容更为方便,不用手输Tag

    dedecms在编辑修改内容时,TAG标签需要手动输,中文的Tag, 中间还得用半角字符','分隔,  输入法切来切去很不方便,   于是动手改后台代码, 利用后台的tags_main.php, 让d ...

  9. phpcms V9首页、列表页以及内容页调用标签

    1.页面标题:{$title} 2.发表时间:{$inputtime} 3.内容来源:{$copyfrom} 4.文章内容:{$content} 5.缩略图地址:{$thumb} 6.组图列表: {l ...

随机推荐

  1. 第三节,入门知识和windows系统安装python环境

    1.使用Linux的好处(稳定)不容易死机,可以长达几年不间断运行(安全)相对windows系统更安全,相对更不容受到各种攻击(开源)免费使用2.安装好虚拟机VMware软件,和Linux系统,以及X ...

  2. Sass入门:第一章

    1.什么是预处理器? CSS预处理器是用一种专门的编程语言,进行Web页面样式设计,然后再编译成正常的CSS文件,以供项目使用.CSS预处理器为CSS增加一些编程的特性,无需考虑浏览器的兼容性问题. ...

  3. C#指定某用户对某文夹件的访问权限

    using System.Security.AccessControl; //设置myFloder文件夹的iis访问权限                string userAccount = @&q ...

  4. Java IO 过滤流 BufferedInput/OutputStream

    Java IO 过滤流 BufferedInput/OutputStream @author ixenos 概念 BufferedInput/OutputStream是实现缓存的过滤流,他们分别是Fi ...

  5. Hibernate配置文件的hbm2ddl.auto属性

    今天遇到一个有意思的问题,我目前做的一个网站采用Spring MVC + Spring + Hibernate的架构,我通过页面插入了一些数据到数据库,可是每次重启tomcat之后,数据都莫名其妙地丢 ...

  6. MySQL 的一个Bug?

    在 my.ini 中的 [mysqld] 段设置 mysql basedir 和 datadir,如下: #Path to installation directory. All paths are ...

  7. redis的适应场景

    redis应用场景: 1.对数据高并发读写 2.对海量数据的高效存储和访问 3.对数据的高可扩展性和高可用性 做分布式扩展很简单,因为没有固定的表结构 redis介绍: redis是一个key-val ...

  8. linux ssh连接不自动断开

    修改linux服务器ssh配置文件: vim /etc/ssh/ssh_config 修改两处的值为: ClientAliveInterval ClientAliveCountMax 使修改的ssh配 ...

  9. (╭ ̄3 ̄)╭ 小希的迷宫II

    (╭ ̄3 ̄)╭ 小希的迷宫II TimeLimit: 2000/1000 MS (Java/Others)  MenoryLimit: 65536/32768 K (Java/Others) 64-b ...

  10. JavaScript实现本地数据简单存取以及Json数据存取

    1.判断本地存储是否可用: if(window.localStorage) { // localStorge可用 }else { // localStorge不可用 } 2.存储数据: // 获取本地 ...