简体   繁体   English

如何在 index.html 中加载另一个 html 点击 Javascript

[英]How to load another html inside index.html on click Javascript

I have a index.html file which was a button.我有一个 index.html 文件,它是一个按钮。 I have a modal.html file which has a custom modal built.我有一个 modal.html 文件,它内置了一个自定义模态。 I want to onclick event render modal.html inside index.html and close it normally.我想onclick事件渲染 modal.html 里面的 index.html 并正常关闭它。 Can anyone help me with the pure Javascript function?任何人都可以用纯 Javascript function 帮助我吗?

index.html below example index.html 下面的例子

<div id="modal-container">
// here I want to load content from modal.html
</div>

modal.html modal.html

<div>
//here is modal content
</div>

There used to be frame tags that allowed you to load other html pages from other addresses.曾经有frame标签允许您从其他地址加载其他 html 页面。 but HTML5 does not support it i guess.但我猜 HTML5 不支持它。

you can create elements and add attributes to create the modal you wish dynamically using js, then find the modal-container element and append it as a child.您可以创建元素并添加属性以使用 js 动态创建您希望的模式,然后找到 modal-container 元素和 append 作为子元素。 then add listeners or queries to open and close it.然后添加侦听器或查询以打开和关闭它。

or use modals provided by Bootstrap或使用Bootstrap提供的模态

or use check this page which is trying to import and use .html files inside another html file或使用检查此页面,该页面试图在另一个 html 文件中导入和使用.html文件

There are multiple ways how to render a .html file into another.有多种方法可以将.html文件渲染到另一个文件中。

Using iframe tag使用iframe标签

An inline frame is used to embed another document within the current HTML document.内联框架用于在当前 HTML 文档中嵌入另一个文档。

<iframe src="modal.html"></iframe>

Using jQuery使用 jQuery

Specifically using jQuery's .get() method:具体使用 jQuery 的.get()方法:

$.get("modal.html", function(data) {
   $("#modal-container").html(data);
});

Using PHP使用 PHP

<?php include 'modal.html'?>

In your case, you could use the jQuery's $.get() method.在您的情况下,您可以使用 jQuery 的$.get()方法。

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

相关问题 如何在 Blazor 客户端索引中加载 JavaScript 文件。html - How to load a JavaScript file in the Blazor client index.html 如何在按钮上单击主索引html内加载html - How to load an html inside main index html on button click 如何在 Javascript 中的另一个 index.html 网页中以文本格式而不是 URL 显示对象属性? - How can I display the object property in text format inside the another index.html webpage in Javascript instead of an URL? index.html 不从 babel 加载转译的 javascript - index.html not load transpiled javascript from babel 从另一个页面重新调整到 index.html 时如何调用 .click() - How to call .click() when retuning to index.html from another page 从另一个页面预加载Index.HTML-Javascript - PreLoad Index.HTML from another Page - Javascript 如果 index.html 没有加载任何 javascript,Vue 应用程序如何启动? - How does a Vue app get launched if the index.html doesn't load any javascript? Angular2:如何使用 Javascript 从 index.html 访问 DOM-Element(组件内部) - Angular2: How to access DOM-Element (inside Component) from index.html with Javascript 如何直接在index.html文件上加载Angular2组件? - How to load an Angular2 component directly on the index.html file? Angular2 - 如何在index.html中有条件地加载css文件? - Angular2 - How to conditionally load css file in index.html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM