简体   繁体   English

用JS导入html模块

[英]Import html module with JS

New to coding and new to JavaScript.编码新手和 JavaScript 新手。

I have a simple web page where I have a menu with different items ( eg, results, and analysis).我有一个简单的网页,其中有一个包含不同项目(例如,结果和分析)的菜单。 When you click on one of the item, say “results”, I want it to display within the same web page (without reloading) a piece of another HTML page containing the results, which is in another file .当您单击其中一个项目时,例如“结果”,我希望它在同一网页中显示(无需重新加载)包含结果的另一个 HTML 页面的一部分,页面位于另一个文件中 Why?为什么? Because the results content will be too large to write it in the main.js , so the html needs to be loaded from a different HTML file.因为results内容会太大,写在main.js ,所以需要从不同的 HTML 文件加载html

I am thinking (but there may be other ways to do it) of importing the results.html with JavaScript with an event listener.我正在考虑(但可能有其他方法可以做到)使用带有事件侦听器的 JavaScript 导入results.html But I'm not sure how to do that.但我不知道该怎么做。 With an HTML module?使用 HTML 模块?

There are different approaches for your Problen:您的问题有不同的方法:

  1. use fetch() to load your data when needed from the server and then render it in to the view.使用fetch()在需要时从服务器加载数据,然后将其呈现到视图中。
  2. use a dynmaic import with import( ... ) and load the whole module including your data and the view使用带有 import( ... ) 的动态导入并加载整个模块,包括您的数据和视图

my personal opinion is to go with 1. and just load your data when you need it.我个人的意见是使用 1. 并在需要时加载您的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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