我的代碼如下創建和附加一個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
標籤中。但是,在運行此操作時出現以下錯誤。
無法執行appendChild
對Node
:參數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>
可能有人請解釋爲什麼發生這種情況,如何解決這個
感謝
可以共享元素 '內容框' HTML? –
內容框的元素是簡單的h3標題和p塊文本。我試圖提取h3元素,並將它們顯示爲另一個div中的段落元素。 – jeff
你可以請創建一個jsfiddle – brk