JavaScript实现动态添加、移除元素或属性的方法分析
本文实例讲述了JavaScript实现动态添加、移除元素或属性的方法。分享给大家供大家参考,具体如下:
JavaScript 动态添加、移除元素
appendChild(newnode)
向节点的子节点列表的末尾添加新的子节点。
insertBefore(newnode, existingnode)
在已有子节点之前插入新的子节点。
removeChild(node)
删除元素的某个指定的子节点,并以 Node 对象返回被删除的节点,如果节点不存在则返回 null。
innerHTML
属性设置或返回表格行的开始和结束标签之间的 HTML。
测试用例
<html> <head> <style type="text/css"> .style1 { background-color:yellow; width:200px;height:100px;float:left;} .style2 { background-color:#aa0; width:200px;height:100px;float:left;} .style3 { background-color:rgb(0,200,200); width:200px;height:100px;float:left;} .item-style {background-color:pink;} </style> <script type="text/javascript"> function addElement1() { var container = document.getElementById("container1"); var elem1 = document.createElement("div"); elem1.setAttribute("class", "item-style"); var textnode1 = document.createTextNode("appendChild"); elem1.appendChild(textnode1); container.appendChild(elem1); var middleChild = document.getElementById("middle-child"); var elem2 = document.createElement("div"); elem2.setAttribute("class", "item-style"); var textnode2 = document.createTextNode("insertBefore"); elem2.appendChild(textnode2); container.insertBefore(elem2, middleChild); }//欢迎加入全栈开发交流圈一起学习交流:582735936 ]//面向1-3年前端人员 } //帮助突破技术瓶颈,提升思维能力 function addElement2() { var container = document.getElementById("container2"); container.innerHTML = "<div>Hello World \"2\"</div>"; } function removeNode() { var container = document.getElementById("container3"); var myNode = document.getElementById("myNode"); container.removeChild(myNode); } function operateElements() { addElement1(); addElement2(); removeNode(); } </script> </head> <body onload="operateElements()"> <div id="container1" class="style1"> <div id="middle-child">Middle Child</div> </div> <div id="container2" class="style2"></div> <div id="container3" class="style3"><p id="myNode">Hello World</p></div> <div style="clear:both;"/> <button onclick="operateElements()">Operate Elements</button> </body> </html>
JavaScript 动态添加、移除属性
setAttribute(attributename, attributevalue)
添加指定的属性,并为其赋指定的值。将属性设置为undefined等同于删除。
removeAttribute(attributename)
删除指定的属性。
getAttributeNode(attributename)
以 Attr 对象返回指定属性名的属性值。
removeAttributeNode(attributenode)
删除 Attr 形式指定的属性,同时返回被删除的Attr 形式的属性。
测试用例
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> function operateAttributes() { var node2 = document.getElementById("node2"); //设置font-style和font-size无效,这些属性脱离style单独设置是无效的 node2.setAttribute("style", "color:red;"); var node3 = document.getElementById("node3"); node3.setAttribute("font-size", undefined); var node4 = document.getElementById("node4"); //font-style和font-size的remove无效,因为它们没有单独存在 node4.removeAttribute("font-size"); var node5 = document.getElementById("node5"); //无法获取font-style和font-size var attributeNode = node5.getAttributeNode("style"); var attr = node5.removeAttributeNode(attributeNode); node5.innerHTML = "attr=" + attr + ", attr.name=" + attr.name + ", attr.value=" + attr.value; } </script> </head> <body onload="operateAttributes()"> <p id="node0" style="font-style:italic;font-size:12px;">0 Hello World</p> <p id="node1" font-size="12px" font-style="italic">1 Hello World : font-size、font-style等,这些属性脱离style单独设置是无效的</p> <p id="node2" style="font-style:italic;font-size:12px;">2 Hello World setAttribute</p> <p id="node3" style="font-style:italic;font-size:12px;">3 Hello World setAttribute</p> <p id="node4" style="font-style:italic;font-size:12px;">4 Hello World removeAttribute</p> <p id="node5" style="font-style:italic;font-size:12px;">5 Hello World getAttributeNode & removeAttributeNode</p> </body> </html>
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Hadoop文件系统元数据管理机制
edits log 默认是 64MB,当写满的时候或者到一定周期的时候,Namanode就会进行 CheckPoint。Checkpoint是一个内部事件,这个事件激活以后会触发数据库写进程(DBWR)将数据缓冲(DATABUFFER CACHE)中的脏数据块写出到数据文件中。 这里仅仅是以一份副本来描述,实际上默认是切片后每一个切片的数据块都有三份副本,保存在不同的Datanode中,假设有多个不同的机架,每个机架有多台主机,意味着有多个机架上面的Datanode,每一台主机作为一个Datanode,数据块的副本保存顺序是: 1、先在本机架上面找寻最近的一台主机保存第一份副本; 2、然后到其他机架上面随机选择一台主机保存第二份副本; 3、最后再在本机架上面除第一份副本的主机外随机选择另一台主机保存第三份副本。 若是Namenode宕机了,还能否恢复数据?重启集群之后还能提供服务么? 可以恢复数据,通过FSimage恢复数据;Namenode宕机了提供不了查询数据和保存数据的功能,因此不能提供服务。 可以使用 多个Namenode副本,副本namenode与namenode的数据保持一...
- 下一篇
Spring源码解析-applicationContext.xml加载和bean的注册
Spring源码解析-AutowiredAnnotationBeanPostProcessor依赖注入 applicationContext文件加载和bean注册流程 Spring对于从事Java开发的boy来说,再熟悉不过了,对于我们这个牛逼的框架的介绍就不在这里复述了,Spring这个大杂烩,怎么去使用怎么去配置,各种百度谷歌都能查到很多大牛教程,但是,当我们按着教程一步步的把spring的开发框架搭建起来的时候,有没有一种想搞明白spring的冲动,万事开头难,就要从开头开始,而我认为spring开头就是如何加载配置文件,并初始化配置文件里面的bean当然也包括了我们用注解Service、Component等注解注解的bean,spring在容器启动的时候就要去加载这些内容,然后统一管理这些bean(统一管理的是他们的bean definition),这也就是spring的一个重要概念bean的容器。 applicationContext.xml到底是如何加载的呢?我把他简化成以下流程,当然了每个环节里Spring的实现都是错综复杂的,也是很佩服写Spring的大神。 S...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Linux系统CentOS6、CentOS7手动修改IP地址
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS7安装Docker,走上虚拟化容器引擎之路
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- 设置Eclipse缩进为4个空格,增强代码规范
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- 2048小游戏-低调大师作品