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. Kubernetes-2.组件

    内容主要摘自官网文档资料 官网地址 本文概述了交付正常运行的Kubernetes集群所需的各种组件. 本文编写基于kubernetes v1.17版本 目录 Kubernetes集群 Master组件 ...

  2. 看完就懂--CSS选择器优先级的计算

    CSS选择器优先级的计算 什么是选择器的优先级 优先级的计算与比较(一) - 优先级具有可加性 - 选择器优先级不会超过自身最大数量级 - 同等优先级情况下,后写的覆盖前写的 - 并集选择器之间的优先 ...

  3. Spring-06 AOP

    Spring-06 AOP AOP 1.简介 AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AO ...

  4. JVM 中的StringTable

    是什么 字符串常量池是 JVM 中的一个重要结构,用于存储JVM运行时产生的字符串.在JDK7之前在方法区中,存储的是字符串常量.而字符串常量池在 JDK7 开始移入堆中,随之而来的是除了存储字符串常 ...

  5. 【Azure API 管理】APIM CORS策略设置后,跨域请求成功和失败的Header对比实验

    在文章"从微信小程序访问APIM出现200空响应的问题中发现CORS的属性[terminate-unmatched-request]功能"中分析了CORS返回空200的问题后,进一 ...

  6. NoSQL安装与操作

    redis操作: redis的启动与关闭:注意:(需要关闭防火墙) redis的启动:redis-server redis.conf redis的登录:redis-cli -a pass redis的 ...

  7. C语言float和double输入问题

    统计给定的n个数中,负数.零和正数的个数. Input    输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数:如果n=0,则表示输入结 ...

  8. [NOIP 2020] 微信步数

    一.题目 点此看题 二.题目 首先感谢一下这位大佬的博客,虽然我看不懂您的讲解,但是还是读得懂代码的 思路是 \(\tt jys\) 给我讲明白的,首先我们可以感觉到快速计算它肯定和矩形有关系,也就是 ...

  9. 关于HDFS存储元数据的NameNode持久化存储

    NameNode持久化场景引入: 问题:NameNode宕机,导致内存中的文件元数据丢失怎么办?我们知道元数据是存储来内存中的,所以一旦宕机,内存数据是会丢失的,因此为了避免数据丢失,HDFS中出现了 ...

  10. 谈谈C++中的数据对齐

    对于C/C++程序员来说,掌握数据对齐是很有必要的,因为只有了解了这个概念,才能知道编译器在什么时候会偷偷的塞入一些字节(padding)到我们的结构体(struct/class),也唯有这样我们才能 ...