document.createElement()创建元素的方式有所不同? [英] document.createElement() creating elements differently?

查看:92
本文介绍了document.createElement()创建元素的方式有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建 div 之类的元素时,它也会创建封闭标签.但是,当我创建像 canvas 这样的元素时,只会创建一个开始标记,而不创建闭合标记.为什么?

When I create an element like div, it creates closed tag also. But when I create an element like canvas only creates an opening tag but not closed tag. Why?

document.createElement('div');

这会创建< div></div>

document.createElement('canvas');

创建< canvas> ,如果我将子元素附加到其上,则它会创建< canvas> childTag</canvas> .有什么特别的原因吗?哪些元素的行为如此?和什么元素的行为类似于 div 之上? canvas 的结束标记是可选的吗?

creates <canvas>, if I append child to it, then it creates <canvas>childTag</canvas>. Is there any particular reason? What elements behave like this? and What elements behave like above div? Did the closing tag for canvas is optional?

推荐答案

这仅在您的开发工具标记实时视图"中.结束标签在那里,必须必须在那里.

This is only in your dev tools "markup Live View". The closing tag is there and must be there.

Chrome可能这样做是因为它不会呈现任何内容(而< canvas> 可能具有一些隐藏的内容).

Chrome probably does this because it won't render any content (while <canvas> may have some hidden content).

您可以通过调用所创建元素的 outerHTML 属性来进行检查:

You can check this by calling the outerHTML property of your created element :

console.log(document.createElement('canvas').outerHTML);

这篇关于document.createElement()创建元素的方式有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆