博客
关于我
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/

    你可能感兴趣的文章
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm和yarn的使用对比
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    NPOI利用多任务模式分批写入多个Excel
    查看>>
    NR,NF,FNR
    查看>>
    nrf开发笔记一开发软件
    查看>>
    NSDateFormatter的替代方法
    查看>>
    nsis 安装脚本示例(转)
    查看>>
    NSOperation基本操作
    查看>>
    NSSet集合 无序的 不能重复的
    查看>>
    NT AUTHORITY\NETWORK SERVICE 权限问题
    查看>>
    ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
    查看>>
    ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
    查看>>
    Nuget~管理自己的包包
    查看>>
    nullnullHuge Pages
    查看>>
    NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值
    查看>>
    numpy 用法
    查看>>
    Numpy如何使用np.umprod重写range函数中i的python
    查看>>
    oauth2-shiro 添加 redis 实现版本
    查看>>