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. DRF 外键字段深度查询优化、ListSerializer辅助完成群改

    目录 一.Response封装 二.外键字段深度查询 1.序列化配置exclude.depth 2.模型层函数.插拔式字段查询 三.listserializer辅助类 一.Response封装 用de ...

  2. Springboot项目架构设计

    导航 前言 流水线 架构的艺术 项目架构 理解阿里应用分层架构 superblog项目架构 结语 参考 本节是<Spring Boot 实战纪实>的第7篇,感谢您的阅读,预计阅读时长3mi ...

  3. matplotlib工具栏源码探析三(添加、删除自定义工具项)

    转: matplotlib工具栏源码探析三(添加.删除自定义工具项) matplotlib工具栏源码探析二(添加.删除内置工具项)探讨了工具栏内置工具项的管理,除了内置工具项,很多场景中需要自定义工具 ...

  4. OpenGL光照贴图

    一:啥叫贴图 上一节中,我们将整个物体的材质定义为一个整体,但现实世界中的物体通常并不只包含有一种材质,而是由多种材质所组成. 拓展之前的系统,引入漫反射和镜面光贴图(Map).这允许我们对物体的漫反 ...

  5. 基于ABP框架的SignalR,使用Winform程序进行功能测试

    在ABP框架里面,默认会带入SignalR消息处理技术,它同时也是ABP框架里面实时消息处理.事件/通知处理的一个实现方式,SignalR消息处理本身就是一个实时很好的处理方案,我在之前在我的Winf ...

  6. JVM笔记 -- JVM经历了什么?

    Sun Classic VM 世界上第一款商用 Java 虚拟机,JDK1.4 已经淘汰. 内部只有解释器,可以自己外挂JIT编译器,但是二者只能使用其一,不能配合工作. hotspot 内置了该虚拟 ...

  7. js 算数组平均值、最大值、最小值、偏差、标准差、中位数、数组从小打大排序、上四分位数、下四分位数

    要算的数组命名为data var sum = function(x,y){ return x+y;}; //求和函数 var square = function(x){ return x*x;}; / ...

  8. protobuf基于java和javascript的使用

    目录 ProtoBuf介绍 整理下java和JavaScript的例子 demo测试 java作为服务端+客户端测试 客户端前端调用示例 项目地址 参考 ProtoBuf介绍 ProtoBuf 是go ...

  9. Ignatius and the Princess III HDU - 1028

    题目传送门:https://vjudge.net/problem/HDU-1028 思路:整数拆分构造母函数的模板题 1 //#include<bits/stdc++.h> 2 #incl ...

  10. MyBatis:当表字段名和实体类属性名不一致

    第一种解决方法:在sql中使用别名 <select id="getRoleList" resultType="com.ttpfx.domain.Role" ...