The elements are being cloned, but you aren't doing anything with the cloned nodes, so visually , nothing happens.(元素正在被克隆,但是您对克隆的节点不做任何事情,因此从视觉上看 ,什么也没有发生。)
.cloneNode
returns the cloned node.(.cloneNode
返回克隆的节点。) If you want to append it to the DOM, you have to do so explicitly - the interpreter isn't going to insert it anywhere automatically:(如果要将其附加到DOM,则必须明确地这样做-解释器不会自动将其插入任何地方:)
document.querySelectorAll(".allEnglishSections").forEach((element) => { document.body.appendChild( element.cloneNode(true) ); });
<section class="allEnglishSections"> <h2>Conditioning</h2> <table> <tr> <th>English</tr> </th> <tr> <td>if</td> </tr> <tr> <td>else</td> </tr> </table> </section> <section class="allEnglishSections"> <h2>Querying</h2> <table> <tr> <th>English</tr> </th> <tr> <td>In plea</td> </tr> <tr> <td>In suggestion</td> </tr> </table> </section>
(Also note that your current code requires deep
to be a variable which has true
assigned to it)((还请注意,您当前的代码要求deep
必须是为其分配了true
的变量))
For very similar reasons, the following code won't do anything by itself:(出于非常相似的原因,以下代码本身不会执行任何操作:)
const div = document.createElement('div');
div.textContent = 'foo';
You'd have to insert the div somewhere for it to be made visible to the user.(您必须将div插入某个位置,以使其对用户可见。)
undefined
is shown in the console because the last expression evaluated gets printed to the console, but forEach
returns undefined
.(在控制台中显示undefined
,因为最后计算的表达式被打印到控制台,但是forEach
返回undefined
。) It's not an error, just an indication that the final expression didn't hold a value.(这不是错误,只是表明最终表达式不包含值。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…