我正在尝试动态创建一个Figure元素,并向其附加一个figcaption。
var newFigure = document.createElement("figure");
var newPictureCaption = document.createElement("figcaption");
$(newPictureCaption).html(imgcaption); //this line is just so the caption isnt blank
$(newFigure).append(newPictureCaption);
但是,我得到了错误
Uncaught DOMException: Failed to execute 'appendChild' on 'Node':
Nodes of type 'figcaption' may not be inserted inside nodes of type '#document-fragment'
答案 0 :(得分:0)
这可能是jQuery问题,转换为原始JavaScript后,您的代码可以按预期工作。我尝试了一种typeof测试,但是并没有显示太多。
var newFigure = document.createElement("figure");
var newPictureCaption = document.createElement("figcaption");
var targetElement = document.getElementById("test");
newFigure.appendChild(newPictureCaption);
targetElement.appendChild(newFigure);
figure {
display: inline-block;
width: 50px;
height: 50px;
background-color: blue;
}
<div id="test"></div>