简体   繁体   English

创建DOM元素并将其与其他元素的内容一起附加

[英]Creating and appending a DOM element with the content of another element

My code is as follows 我的代码如下

  ;(function(window){
    var $description_window= document.querySelector('.mg_post_description'),
        $headings= document.querySelectorAll('.mg_blog_main_content h3'),
        $paragraph = document.createElement('p');
        for (var j = 0; j < $headings.length; j++) {
            var $headingChildren=$headings[j].cloneNode(true).childNodes;
            $paragraph.appendChild($headingChildren[j]);
       }
       $description_window.append($paragraph);
})(window);

Here what I am trying to do is; 我在这里想做的是 to copy the h3 tags of the content box. 复制内容框的h3标签。 Then create a paragraph element. 然后创建一个paragraph元素。 Then append the p tags with collected h3 tags. 然后在p标签h3附加收集的h3标签。 However, I get the following error on running this. 但是,运行此程序时出现以下错误。

Failed to execute appendChild on Node : parameter 1 is not of type Node . 无法在Node上执行appendChild :参数1的类型不是Node

  ;(function(window){ var $description_window= document.querySelector('.post_description'), $headings= document.querySelectorAll('.post_description h3'), $paragraph = document.createElement('p'); for (var j = 0; j < $headings.length; j++) { var $headingChildren=$headings[j].cloneNode(true).childNodes; $paragraph.appendChild($headingChildren[j]); } $description_window.append($paragraph); })(window); 
 .post_description{ width:200px; height:200px; background-color:#555; position:absolute; top:10%; right:8%; } .post_description a { color:white; } .main_body{ padding-top:40%; } 
 <div class="main_body"> <h3>Testing one</h3> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <h3>Testing two</h3> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <h3>Testing three</h3> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <h3>Testing four</h3> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div> <div class="post_description"></div> 

Could someone please explain why this occurs and how to solve this 有人可以解释为什么会发生这种情况以及如何解决这个问题

Thanks 谢谢

To achieve expected result, use below option 为了达到预期的效果,请使用以下选项

function addElem() {
    var mElem = document.querySelector('.mg_post_description')
    var hElems = document.getElementsByTagName('H3');
    var node = document.createElement("P");
    for (var i = 0; i < hElems.length; i++) {

        var textnode = document.createTextNode(hElems[i].innerHTML);
        var cloneElem = node.cloneNode(true);
        cloneElem.appendChild(textnode);
        mElem.appendChild(cloneElem);
    }
};

addElem()

Codepen- https://codepen.io/nagasai/pen/YVEzdJ Codepen- https: //codepen.io/nagasai/pen/YVEzdJ

 var d = document.querySelector('.desc'), h = document.querySelectorAll('.main h3'); h.forEach(function(n) { var p = document.createElement('p'); p.innerHTML = n.cloneNode(true).innerHTML || ''; d.append(p); }); 
 .desc { color: red; } 
 <div class="main"> <h3>First</h3> <h3>Second</h3> <h3>Third</h3> </div> <div class="desc"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM