如何在HTML中清晰地更新大元素的内容?

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

How do I cleanly update a large element's content in HTML?

问题

我正在开发一个网站,允许用户通过生成器的多个阶段,我希望将这个过程放在一个HTML页面上进行。我目前尝试的方法是通过更新一个div的innerHTML来实现,但这样会变得非常冗长和复杂。是否有一种方法可以将内容编写在单独的HTML文件中,然后在调用JavaScript函数时导入它并更新元素?

function stepTwo() {
    document.getElementById("mainText").innerHTML = `
    <h3>第二个问题</h3>
    <select id="gender">
        <option value="male">男性</option><option value="female">女性</option>
        <option value="non-binary">非二元</option>...
    </select>
    <a 大量更多的行>
    `
}

显然,这样的代码看起来很不优雅。我正在寻找任何一种方法来简化它,无论是通过导入另一个HTML文件还是其他方式。感谢您的帮助!

英文:

I'm working on a website that lets the user go through several stages of a generator, and I want to do it on one HTML page. The current way I'm trying to do this is by updating a div's innerHTML, but that gets very long and complicated fast. Is there a way to write the content in a separate HTML file, then import it & update the element when a javascript function is called?

function stepTwo() {
    document.getElementById(&quot;mainText&quot;).innerHTML = `
    &lt;h3&gt;Question two&lt;/h3&gt;
    &lt;select id = &quot;gender&quot;&gt;
        &lt;option value = &quot;male&quot;&gt;Male&lt;/option&gt;&lt;option value = &quot;female&quot;&gt; Female&lt;/option&gt;
        &lt;option value = &quot;non-binary&quot;&gt;Non-binary&lt;/option&gt;...
    &lt;/select&gt;
    &lt;a LOT more lines&gt;
    `
}

Obviously this is very ugly. I'm looking for any way to clean it up, be it by importing another HTML file or some other way. Thanks for your help!

答案1

得分: 1

是的,您可以将内容分隔到单独的HTML文件中,并使用JavaScript将它们动态加载到主HTML页面中。

  1. 为生成器的每个阶段创建单独的HTML文件

  2. 在您的主HTML文件中,创建一个<div>或任何其他容器元素

  3. 您可以使用AJAX从单独的HTML文件加载内容,并在调用函数时更新内容容器。

function loadStage(stageNumber) {
  $.ajax({
    url: 'stage' + stageNumber + '.html', // 替换为正确的文件名
    dataType: 'html',
    success: function(data) {
      $('#contentContainer').html(data);
    },
    error: function() {
      console.log('无法加载第' + stageNumber + '阶段的内容。');
    }
  });
}

loadStage(1);

这有助于简化您的代码并使其更容易管理生成器的每个阶段的内容。

英文:

Yes, you can separate the content into separate HTML files and dynamically load them into your main HTML page using JavaScript.

  1. Create separate HTML files for each stage of the generator

  2. In your main HTML file, create a <div> or any other container element

  3. you can use AJAX to load the content from the separate HTML files and update the content container when a function is called.

function loadStage(stageNumber) {
  $.ajax({
    url: &#39;stage&#39; + stageNumber + &#39;.html&#39;, // Replace with the correct file name
    dataType: &#39;html&#39;,
    success: function(data) {
      $(&#39;#contentContainer&#39;).html(data);
    },
    error: function() {
      console.log(&#39;Failed to load stage &#39; + stageNumber + &#39; content.&#39;);
    }
  });
}

loadStage(1);

This helps simplify your code and makes it easier to manage the content for each stage of your generator

huangapple
  • 本文由 发表于 2023年6月5日 23:04:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407752.html
匿名

发表评论

匿名网友

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

确定