如何将一个`div`添加到另一个HTML文档的body中?

huangapple go评论57阅读模式
英文:

How can I append a `div` in a HTML to another HTML's body?

问题

You can append the content from part.html to main.html using JavaScript like this:

// Assuming you have loaded the content of 'part.html' and 'main.html' into variables, for example:
var partContent = '<div id="GBVX1WkdGsQT1b36AbVC" class="plotly-graph-div" style="height:100%; width:100%;"></div>';
var mainContent = '<!doctype html><html><head></head><body></body></html>';

// Create a temporary element to hold the 'part.html' content
var tempElement = document.createElement('div');
tempElement.innerHTML = partContent;

// Append the 'div' from 'part.html' to the 'body' of 'main.html'
document.body.appendChild(tempElement.querySelector('div'));

// Now 'main.html' has the 'div' from 'part.html' appended to its body

This JavaScript code extracts the div element from part.html and appends it to the body of main.html.

英文:

I have the following part.html file, which contains a div and script:

&lt;div id=&quot;GBVX1WkdGsQT1b36AbVC&quot; class=&quot;plotly-graph-div&quot; style=&quot;height:100%; width:100%;&quot;&gt;&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    Plotly.newPlot(&quot;GBVX1WkdGsQT1b36AbVC&quot;, {
  &quot;data&quot;: [],
  &quot;layout&quot;: {},
  &quot;config&quot;: {}
});
&lt;/script&gt;

And I have a main.html, which is the file I am rendering on the web page:

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

How can I append the content from part.html to main.html with using Javascript?

答案1

得分: 2

你可以使用 fetch() 来获取 part.html 中的文本,然后将 document.body.innerHTML 设置为该文本。像这样:

main.html

<!doctype html>
<html>
<head>
    <script>
        fetch('part.html') // 这可以是任何HTML文件的URL
            .then(res => res.text())
            .then(text => document.body.innerHTML = text);
    </script>
</head>
<body>
</body>
</html>

part.html

<!-- 仅供演示目的,可以是任何内容 -->
<h1>测试</h1>

这也可以很容易地适应使用多个部分,只需进行多个 fetch() 调用,并在每次调用中执行 document.body.innerHTML += text

英文:

You can use fetch() to get the text of part.html, then set document.body.innerHTML to that text. Like this:

main.html

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;script&gt;
        fetch(&#39;part.html&#39;) // this can be the URL of any HTML file
            .then(res =&gt; res.text())
            .then(text =&gt; document.body.innerHTML = text);
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

part.html

&lt;!-- Just for demo purposes, can be anything --&gt;
&lt;h1&gt;Test&lt;/h1&gt;

This can also be easily adapted to use multiple parts by making multiple fetch() calls and just doing document.body.innerHTML += text each time instead.

huangapple
  • 本文由 发表于 2023年4月17日 09:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定