我正在使用以下 D3.js 教程:https ://www.tutorialspoint.com/d3js/index.htm 。
我的問題如下:
- 我知道 HTML 中的位置位于 . 我的意思是,我通常把它放在這里:
<body>
<!-- HTML code -->
<script>
<!-- JS code or external JS link -->
</script>
</body>
通過這種做法,我正在尋找的是在 HTML 內容呈現后運行 JS。
但!當我使用 D3.js 遵循這種做法時,我發現 D3.js 在腳本標記之后呈現了我添加的內容(使用 d3("html").append(something to append))。
例如!
<!DOCTYPE html>
<head>
<title>D3.js Test</title>
</head>
<body>
<div class="div_test">
<h1>I want the D3.js content after this div (but not inside the div)</h1>
</div>
<script type = "text/javascript" src = "https://d3js.org/d3.v4.min.js"></script>
<script>
d3.select("html").append("p").text("I get this text after the script tags");
</script>
</body>
</html>
我得到的內容如下:
<!DOCTYPE html>
<html>
<head>
<title>D3.js Test</title>
</head>
<body>
<div class="div_test">
<h1>I want the D3.js content after this div (but not inside the div)</h1>
</div>
<script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
<script>
d3.select("html").append("p").text("I get this text after the script tags");
</script>`
</body><p>I get this text after the script tags</p></html>
問題!
- 標簽的位置是否正確?
- 是否有可能在不添加 a 來錨定預期的新標簽的情況下保持流量?
謝謝!!!
uj5u.com熱心網友回復:
您可以使用selection.insert()插入元素而不是將其附加到 DOM。該方法的第二個引數決定了新創建的元素在 DOM 樹中的位置。如果您只想將其放在第一個<script>元素的前面,您可以執行以下操作:
d3.select("body").insert("p", "script") // <-- insert before first <script>
如果您需要將它放在 之后<div>,無論下一個元素可能是什么樣子,您都可以使用相鄰的兄弟組合器來選擇直接跟在<div>like 之后的兄弟元素,如下所示:
d3.select("body").insert("p", "div *") // <-- insert before next element following div
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/447918.html
標籤:javascript html d3.js 工作流程
上一篇:在CSS中調整文本位置
下一篇:如何正確在文本區域上放置按鈕?
