简体   繁体   English

单击链接时如何在同一页面上显示另一个HTML文件的内容?

[英]How to show another HTML file's contents on the same page when a link is clicked?

当我在页面左侧有几个链接时,如何在单击链接时在同一页面上显示另一个HTML文件的内容?

Simple solution: iframe s, nicer but slightly harder solution: AJAX . 简单的解决方案: iframe ,更好但更难的解决方案: AJAX

  • iFrame iFrame
    To create an iframe put this in your source code: <iframe src="the/URL/of/a/page.html"></iframe> . 要创建iframe,请将其放入您的源代码中: <iframe src="the/URL/of/a/page.html"></iframe> That would display the file at the/URL/of/a/page.html in the iframe. 这将在iframe中的the/URL/of/a/page.html中显示文件。 An iframe can be styled with CSS (just FYI). 可以使用CSS(仅供参考)设置iframe的样式。 Then give all your <a> sa class, I took link for my example JavaScript, and the iframe an ID (I took iframe ). 然后给您所有的<a> sa类,我为示例JavaScript设置了link ,并为iframe设置了ID(我采用了iframe )。 (There is no way to do it without JS AFAIK, or you would have to use a <frameset> , which is deprecated.) The example JS: (如果没有JS AFAIK,则无法做到这一点,否则您将不得不使用已废弃的<frameset> 。)示例JS:

     var links = document.getElementsByClassName("link"); // Get all the elements with the class link for (var i; i < links.length; i++) { // Loop over all the links links[i].onclick = function(e) { // Tell them what to do when they're clicked e.preventDefault(); // Prevent the browser from navigating to the address of the links href document.getElementById("iframe").src = links[i].href; // Set the iframe's src to the href of the a. } } 
  • AJAX AJAX
    That's a little bit harder, and using a library like jQuery simplifies this task enormously. 这有点困难,使用jQuery之类的库极大地简化了此任务。 In my example I use jQuery, otherwise it would have taken much more lines of code. 在我的示例中,我使用jQuery,否则它将花费更多的代码行。
    You need a div to display the loaded page, put one on your page and give it an ID (I used resultbox in my example). 您需要一个div来显示加载的页面,在页面上放一个并为其指定ID(在示例中使用了resultbox )。 Note that AJAX normally only works with files from the same domain, contrary to the iframe solution (although I believe I saw some workarounds/hacks on SO). 请注意,与iframe解决方案相反,AJAX通常仅适用于同一域中的文件(尽管我相信我在SO上看到了一些解决方法/漏洞)。 The example: 这个例子:

     $(".link").click(function(e) { // If an element with the class 'link' get's clicked... e.preventDefault(); $("#resultbox").load("the/URL/of/a/page.html"); // ... load the contents of the file at the/URL/of/a/page.html into the element with the ID 'resultbox' }); 

Note: I didn't test any of this. 注意:我没有测试任何一个。

暂无
暂无

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

相关问题 单击导航时不重新加载页面,将.html内容替换为另一个.html - No page reloading when navigation is clicked, replacing .html contents with another .html 活动链接不显示刷新页面时添加的类名称,也不显示从其他页面单击相同链接时添加的类名称 - Active links don't show the class name added when the page is refreshed or when the same link is clicked from another page 单击链接后,获取另一个类的内容 - When a link is clicked, grab the contents of another class 如何将单击链接的值传递到另一个页面并在跨度中显示它 - How to pass a value of a clicked link to another page and show it in a span 当单击另一个选项卡(链接)并且用户留在同一页面上时,如何防止 CSS 发生变化? - How to prevent CSS from changing when another tab(link) is clicked and the user is remaining on the same page? 单击链接时如何在同一页面上显示框 - how to display box on same page when a link is clicked 点击链接时需要显示弹出页面 - Need to show popup page when clicked the link 在html页面中单击时如何在对话框中显示相同的图像? - how to display the same image in dialog box when it is clicked in a html page? html:单击链接时执行功能,获取警报,然后重定向到另一页 - html: execute function when link clicked, get alert and then redirect to another page 如何显示确认框<link to="###">被点击 - How to show a confirmation box when a <link to="###"> is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM