2017-05-06 139 views
0

我的代码如下创建和附加一个DOM元素与其它元素

;(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); 

这里就是我想要做的是内容;复制内容框的h3标签。然后创建一个paragraph元素。然后将p标签附加到收集的h3标签中。但是,在运行此操作时出现以下错误。

无法执行appendChildNode:参数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>

可能有人请解释为什么发生这种情况,如何解决这个

感谢

+0

可以共享元素 '内容框' HTML? –

+0

内容框的元素是简单的h3标题和p块文本。我试图提取h3元素,并将它们显示为另一个div中的段落元素。 – jeff

+0

你可以请创建一个jsfiddle – brk

回答

1

为了实现预期的结果,请使用下列选项

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

+0

它确实有效。但是,我可以问我是否可以通过修改我在此发布的原始代码来做同样的事情。感谢和期待 – jeff

+0

更新了codepen-https://codepen.io/nagasai/pen/ZKaEZe,希望这对你有用:) –

+0

谢谢你的帮助 – jeff

0

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>

+0

这一个复制新div中的h3元素,而不是p元素。 – jeff

+0

您能否在p标签中生成重复元素?感谢 – jeff

+0

这样的事情? – Dylan