博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用JavaScript将图像添加到DOM
阅读量:2509 次
发布时间:2019-05-11

本文共 736 字,大约阅读时间需要 2 分钟。

I had the need to programmatically add an image to the , in other words to an HTML page, dynamically.

我需要以编程方式动态地将图像添加到 ,即添加到HTML页面。

To do that, I created an img element using the createElement method of the Document object:

为此,我使用Document对象的createElement方法创建了一个img元素:

const image = document.createElement('img')

Then I set the src attribute of the image:

然后设置图像的src属性:

image.src  = '/picture.png'

(You can use a relative or an absolute URL, just as you’d use in a normal HTML img tag)

(您可以使用相对或绝对URL,就像在普通HTML img标签中使用的一样)

Then I identified the container I wanted to append the image to, and I called the appendChild() method on it:

然后,我确定了要将图像附加到的容器,并在其上调用了appendChild()方法:

document.querySelector('.container').appendChild(image)

翻译自:

转载地址:http://nomgb.baihongyu.com/

你可能感兴趣的文章
2020-11-18
查看>>
Docker面试题(二)
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
注册用户
查看>>
TZC Intercommunication System
查看>>
HDU 4571 SPFA+DP
查看>>
centos 创建以日期为名的文件夹
查看>>
Java Timer触发定时器
查看>>
Page Object设计模式
查看>>
程序的基础知识
查看>>
在VIM中使用GDB调试 – 使用vimgdb
查看>>
python爬虫---从零开始(五)pyQuery库
查看>>
POJ2236(KB5-A)
查看>>
Centos MySQL数据库迁移详细步骤
查看>>
2初出茅庐--初级篇2.1
查看>>
新建 WinCE7.0 下的 Silverlight 工程
查看>>
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>