简体   繁体   English

有没有办法在我的主 html 或 javascript 中调用 another.html 文件

[英]Is there a way to call another .html file in my main html or in javascript

I was trying this find this out but too no avail.我正在尝试找出这个,但也无济于事。 I was just wondering if you could call another separate html file in your main html.我只是想知道您是否可以在主 html 中调用另一个单独的 html 文件。

I know you can do it for javascript <script src = "Classroom_names.js"></script>我知道你可以做到 javascript <script src = "Classroom_names.js"></script>

So I was wondering if you could do it also for it in html. For example所以我想知道你是否也可以在 html 中为它做这件事。例如

<script src = "field_names.html"></script>

Or if you could not do it like that would you be able to call a html file in javascript andthen set a condition to it.或者,如果您不能那样做,您是否可以调用 javascript 中的 html 文件,然后为其设置条件。

Thank you for taking your time to read this guys感谢您花时间阅读这些人

The easiest way to do it is using jQuery, which you must include in your project, and then use the load function to place it in some node of your website, in this case I used a div with the class "menuContainer" and the menu I have put in a file called menu.html最简单的方法是使用 jQuery,您必须将其包含在您的项目中,然后使用负载 function 将其放置在您网站的某个节点中,在这种情况下,我使用了一个带有 class“menuContainer”和菜单的 div我放入了一个名为 menu.html 的文件

Important: You must have a web server running on localhost to be able to use this function since if you open the file using the url "file: /// your_file.html" through the browser it will give you a cross-origin error, if you don't have a server web running you can mount it very easily with python3 using the http.server module or in nodejs using http-server.重要提示:您必须在本地主机上运行 web 服务器才能使用此 function,因为如果您通过浏览器使用 url“文件:/// your_file.html”打开文件,它会给您一个跨源错误,如果您没有运行服务器 web,您可以使用 http.server 模块使用 python3 非常轻松地安装它,或者使用 http-server 在 nodejs 中安装它。

<!DOCTYPE html>
<html lang="en">
<head>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div class="menuContainer"></div>
</body>

  <script>
    $(document).ready(function () {
      $('.menuContainer').load('./menu.html');
    });
  </script>
</html>

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

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