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

    你可能感兴趣的文章
    OpenStack项目管理实战
    查看>>
    OpenStreetMap初探(一)——了解OpenStreetMap
    查看>>
    openSUSE 13.1 Milestone 2 发布
    查看>>
    openSUSE推出独立 GUI 包管理工具:YQPkg,简化了整个软件包管理流程
    查看>>
    OpenVSwtich(OVS)Vlan间路由实战 附实验环境
    查看>>
    Openwrt LuCI模块练习详细步骤
    查看>>
    OpenWrt固件编译刷机完全总结
    查看>>
    Open××× for Linux搭建之二
    查看>>
    Open×××有线网络时使用正常,无线网络时使用报错的解决方案
    查看>>
    Operation not supported on read-only collection 的解决方法 - [Windows Phone开发技巧系列1]
    查看>>
    Operations Manager 2007 R2系列之仪表板(多)视图
    查看>>
    operator new 与 operator delete
    查看>>
    operator() error
    查看>>
    OPPO K3在哪里打开USB调试模式的完美方法
    查看>>
    Optional类:避免NullPointerException
    查看>>
    ORA-00932: inconsistent datatypes: expected - got NCLOB【ORA-00932: 数据类型不一致: 应为 -, 但却获得 NCLOB 】【解决办法】
    查看>>
    ORA-00942 表或视图不存在
    查看>>
    ORA-01795: 列表中的最大表达式数为 1000
    查看>>
    ORA-06575: 程序包或函数 NO_VM_DROP_PROC 处于无效状态
    查看>>
    ora-12541:tns:no listener
    查看>>