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

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

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:

  1. // Assuming you have loaded the content of 'part.html' and 'main.html' into variables, for example:
  2. var partContent = '<div id="GBVX1WkdGsQT1b36AbVC" class="plotly-graph-div" style="height:100%; width:100%;"></div>';
  3. var mainContent = '<!doctype html><html><head></head><body></body></html>';
  4. // Create a temporary element to hold the 'part.html' content
  5. var tempElement = document.createElement('div');
  6. tempElement.innerHTML = partContent;
  7. // Append the 'div' from 'part.html' to the 'body' of 'main.html'
  8. document.body.appendChild(tempElement.querySelector('div'));
  9. // 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:

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

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

  1. &lt;!doctype html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;/head&gt;
  5. &lt;body&gt;
  6. &lt;/body&gt;
  7. &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

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <script>
  5. fetch('part.html') // 这可以是任何HTML文件的URL
  6. .then(res => res.text())
  7. .then(text => document.body.innerHTML = text);
  8. </script>
  9. </head>
  10. <body>
  11. </body>
  12. </html>

part.html

  1. <!-- 仅供演示目的,可以是任何内容 -->
  2. <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

  1. &lt;!doctype html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;script&gt;
  5. fetch(&#39;part.html&#39;) // this can be the URL of any HTML file
  6. .then(res =&gt; res.text())
  7. .then(text =&gt; document.body.innerHTML = text);
  8. &lt;/script&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;/body&gt;
  12. &lt;/html&gt;

part.html

  1. &lt;!-- Just for demo purposes, can be anything --&gt;
  2. &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:

确定