使用另一个元素的内容创建和附加DOM元素

时间:2017-05-06 06:12:23

标签: javascript dom nodes appendchild createelement

我的代码如下

paragraph

这里我要做的是;复制内容框的h3标签。然后创建一个p元素。然后使用收集的h3标记附加appendChild标记。但是,运行此命令时出现以下错误。

无法在Node上执行Node:参数1不属于 ;(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>
import sys

if sys.version_info[0] >= 3:
    def enoent_doc(func):
        "py3k compat around ENOENT"
        func.__doc__ = func.__doc__.replace('OS:ENOENT',
            'FileNotFoundError: [Errno 2]').replace('IO:ENOENT',
            'FileNotFoundError: [Errno 2]')
        return func
else:
    def enoent_doc(func):
        "py2k compat around ENOENT"
        func.__doc__ = func.__doc__.replace('OS:ENOENT',
            'OSError: [Errno 2]').replace('IO:ENOENT',
            'IOError: [Errno 2]')
        return func

@enoent_doc
def testme():
    """
    Test doctest vs ENOENT

    >>> testme()
    Traceback (most recent call last):
        ...
    OS:ENOENT so far so good
    """
    raise OSError(errno.ENOENT, 'so far so good')

有人可以解释为什么会发生这种情况以及如何解决这个问题

由于

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

n2 = 1;
for (int c = 0; c < n; c++) {
    n2 *= 10;
}
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;
}