jquery添加为什么没办法添加封闭标签

$("#id").append('<img src="src" />');

就像这样添加一个img标签,但是为什么添加进去是没有封闭的呢?
在HTML里是这样的<img src="src">

阅读 3.3k
2 个回答

img属于自动封闭标签

另外你那个js代码可以正常运行? 不应该是

$("#id").append("<img src='src'>");

http://api.jquery.com/jQuery/...

When the parameter has a single tag (with optional closing tag or quick-closing) — $( "<img />" ) or $( "<img>" ), $( "<a></a>" ) or $( "<a>" ) — jQuery creates the element using the native JavaScript .createElement() function.

也就是说不是直接插入的

是先解析html->得出是单个的img->使用createElement

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题