Modifying an existing element

We covered various ways that you can modify aspects of an existing element:

Modifying attributes

You can set an attribute on an element by setting the property of the same name. For example, to change the src of an <img>:
imgEl.src = "http://www.dogs.com/dog.gif";
In addition, you can also use the setAttribute method, like so:
imgEl.setAttribute("src", "http://www.dogs.com/dog.gif");
If you want to remove an attribute, you should do that with removeAttribute - like to remove the disabled attribute off a button, effectively enabling it:
imgEl.removeAttribute("disabled");

Modifying styles

You can change styles just like how you change attributes, by accessing the style property of the element, and setting the corresponding property. For example, to change the color:
headingEl.style.color = "hotpink";
Remember to "camelCase" the multi-word CSS property names, since hyphens are not valid JS property names:
headingEl.style.backgroundColor = "salmon";

Modifying class names

To add a class to an element, you can set the className property:
mainEl.className = "warning";
That will override any existing classes, so you should do this instead if you just want to add to the list of classes:
mainEl.className += " warning";
In newer browsers, you can use the classList functionality instead:
mainEl.classList.add("warning");

Modifying inner HTML / text

To completely replace the contents of an element with an arbitrary string of HTML, use innerHTML:
mainEl.innerHTML = "cats are the <strong>cutest</strong>";
If you don't need to pass in HTML tags, you should use textContent instead:
mainEl.textContent = "cats are the cutest";
Generally, you should be careful when using either of these 2 properties, because they will also remove event listeners (which we teach in the next tutorial).

Creating elements from scratch

There is a whole set of functions that you can use to create entirely new elements and add them to the page.
To create a new element, use the aptly named createElement:
var imgEl = document.createElement("img");
To append it to the page, call appendChild on the target parent element:
document.body.appendChild(imgEl);
Similarly, you can also use insertBeforereplaceChildremoveChild, and insertAdjacentHTML.

Summary: DOM modification techniques的更多相关文章

  1. 高性能Javascript(2) DOM编程

    第三部分 DOM编程 文档对象模型(DOM)是一个独立于语言的,使用XML和HTML文档操作的应用程序接口(API).在浏览器中,主要与HTML文档打交道,在网页应用中检索XML文档也很常见.DOM ...

  2. Techniques for HA IT Management

    7. Techniques That Address Multiple Availability Requirements Redundancy Hardware Redundancy Example ...

  3. 给Extjs的GridPanel增加“合计”行(转)

    再Google,找到一个看似写的比较好的 http://www.cnblogs.com/over140/archive/2009/05/06/1449892.html 期间主要部分也是借鉴官方论坛上的 ...

  4. CCNET+MSBuild+SVN实时构建的优化总结

    本文不是介绍如何使用CCNET+MSBuild+SVN构建自动编译系统,相关的内容可以从很多地方获取,可以再园子里搜一下. 随着我们的SVN库日益壮大,容量达到10G,几十G 甚至更大时,我们发现自动 ...

  5. Offset Management For Apache Kafka With Apache Spark Streaming

    An ingest pattern that we commonly see being adopted at Cloudera customers is Apache Spark Streaming ...

  6. (转) Ensemble Methods for Deep Learning Neural Networks to Reduce Variance and Improve Performance

    Ensemble Methods for Deep Learning Neural Networks to Reduce Variance and Improve Performance 2018-1 ...

  7. JavaFX WebView and WebEngine Tutorial教程

    JavaFX WebView JavaFX WebView is a mini browser that is called as an embedded browser in JavaFX appl ...

  8. ViewContainerRef 动态创建视图

    Angular DOM 操作 相关的APIs和类: 查询DOM节点 template variable ref: 模版变量引用,相当于react中的ref ViewChild: 查询DOM,返回单个元 ...

  9. Review: JQuery

    1.DOM access with jQuery 1 $("h1"); //select all the h1s 2 $("#heading"); // sel ...

随机推荐

  1. SpringBoot注解集合

    使用注解的优势: 1.采用纯java代码,不在需要配置繁杂的xml文件 2.在配置中也可享受面向对象带来的好处 3.减少复杂配置文件的同时亦能享受到springIoC容器提供的功能 @SpringBo ...

  2. ImportError: No module named _ssl解决方法

    import ssl时出现ImportError: No module named _ssl错误是因为咱安装Python的时候没有把ssl模块编译进去导致的. 解决步骤: 系统没有openssl,手动 ...

  3. Java 队列同步器 AQS

    本文部分摘自<Java 并发编程的艺术> 概述 队列同步器 AbstractQueuedSynchronize(以下简称同步器),是用来构建锁(Lock)或者其他同步组件(JUC 并发包) ...

  4. CVE-2019-12409-Apache Solr JMX服务远程代码执行

    漏洞分析 https://www.freebuf.com/vuls/218730.html 漏洞介绍 该漏洞源于默认配置文件solr.in.sh中的ENABLE_REMOTE_JMX_OPTS配置选项 ...

  5. CNN结构演变总结(三)设计原则

    CNN结构演变总结(一)经典模型 CNN结构演变总结(二)轻量化模型 前言: 前两篇对一些经典模型和轻量化模型关于结构设计方面的一些创新进行了总结,在本文将对前面的一些结构设计的原则,作用进行总结. ...

  6. key解析

    密钥在不同实体之间传递,因此密钥必须可以序列化. 所有密钥三个特性: 算法:密钥使用的算法,如DES和DSA等,通过getAlgorithm()获取算法名 编码形式:密钥的外部编码形式,如X.509, ...

  7. 怎么用Markdown在github上写书,并用pages展示

    怎么用git写书 安装环境 第一步 安装node npm 先检测自己电脑是否安装了node npm # 查看 node 版本 node -v # 查看 npm 版本 npm -v 复制代码 如果成功打 ...

  8. java面试记很多次还是记不住的问题

    1.java底层如何实现多态 https://blog.csdn.net/fan2012huan/article/details/51007517 (1)在常量池中找到方法调用的符号引用 (2)查看P ...

  9. flutter兼论

    Flutter是Google开发的一套全新的跨平台.开源UI框架,支持iOS.Android系统开发,并且是未来新操作系统Fuchsia的默认开发套件.自从2017年5月发布 第一个版本以来,目前Fl ...

  10. 正则表达式-Python实现

    1.概述: Regular Expression.缩写regex,regexp,R等: 正则表达式是文本处理极为重要的工具.用它可以对字符串按照某种规则进行检索,替换. Shell编程和高级编程语言中 ...