博客
关于我
JavaScript DOM三种创建元素的方式
阅读量:79 次
发布时间:2019-02-26

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

三种创建元素的方式

  • document.write()
  • 实现代码:

    Document

    段落

    inner
    create
    初始页面:![图片](https://img-blog.csdnimg.cn/20201025155523217.png)实现效果:![图片](https://img-blog.csdnimg.cn/20201025155537308.png)使用document.write()创建元素,如果页面文档流加载完毕,再调用事件会导致页面重绘。2. element.innerHTML(1) 字符串拼接方式实现代码:```html
    Document

    段落

    inner
    create
    实现效果:![图片](https://img-blog.csdnimg.cn/20201025160552254.png)(2) 添加数组元素方式实现代码:```html
    Document

    段落

    inner
    create
    实现效果:![图片](https://img-blog.csdnimg.cn/20201025160518383.png)3. document.createElement()实现代码:```html
    Document

    段落

    inner
    create
    实现效果:![图片](https://img-blog.csdnimg.cn/20201025160733990.png)总结:- document.write()是直接将内容写入页面的内容流,但会导致页面全部重绘。- element.innerHTML是将内容写入某个DOM节点,不会导致页面重绘,可通过字符串拼接或数组方式添加。- document.createElement()创建多个元素,结构更清晰。

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

    你可能感兴趣的文章
    Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
    查看>>
    nginx 反向代理 转发请求时,有时好有时没反应,产生原因及解决
    查看>>
    Nginx 反向代理解决跨域问题
    查看>>
    Nginx 反向代理配置去除前缀
    查看>>
    nginx 后端获取真实ip
    查看>>
    Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
    查看>>
    Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
    查看>>
    Nginx 常用配置清单
    查看>>
    nginx 常用配置记录
    查看>>
    Nginx 我们必须知道的那些事
    查看>>
    Nginx 的 proxy_pass 使用简介
    查看>>
    Nginx 的配置文件中的 keepalive 介绍
    查看>>
    Nginx 负载均衡与权重配置解析
    查看>>
    Nginx 负载均衡详解
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置https(一)—— 自签名证书
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx 配置解析:从基础到高级应用指南
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
    查看>>