简体   繁体   中英

How to attach an HTML element or a DocumentFragment to the DOM?

I've got an HTML fragment like <p>Hello, World!</p> and want to attach it to the container HTML page that includes

<script src="lib/kotlin.js"></script>
<script src="my-app.js"></script>

There are two package that came to my mind:

  • kotlin.js.dom.html.window.document.*
  • kotlin.browser.document.*

Which should I use and how do I access the document's root? I've already tried document.getElementById("container") whereby container is the id of a DIV. But this returns null . I also tried document.appendChild(node) ...

Also which of the above packages should I prefer?

I just figured out that the JS output of the compiled app needs to be below the element that is referenced inside the app.

I've created a demo case that illustrates this :

<!DOCTYPE html>

<meta charset="utf-8"/>
<script src="lib/kotlin.js"></script>

<p>Look inside the JavaScript Console of your browser…</p>
<div id="container"></div>
<script>
    console.log("Native JavaScript");
</script>
<!-- This script tag was misplaced. It needs to be placed after all elements you want to access in your DOM -->
<script src="kotlin-javascript-hello-world.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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