javascript 是否可以使用 DIV 而不是 iFrame 使内容看起来像在同一页面中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20660953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:59:14  来源:igfitidea点击:

Is it possible to use a DIV instead of an iFrame so that content looks like in the same page?

javascriptjqueryhtmliframe

提问by bard

I have a web page, I need part of the content separated to an individual page, but not use iframe. Just like to know if it is possible use div instead of iframe and works as it in the same page(With js base code or php code is cool).

我有一个网页,我需要将部分内容分离到一个单独的页面,但不使用 iframe。只是想知道是否可以使用 div 而不是 iframe 并在同一页面中使用它(使用 js 基本代码或 php 代码很酷)。

I'd need the whole content of <div class="row-fluid">..</div>to be an individual html, but not use iframe, I will need a div instead of iframe, and make it works like just as in one page.

我需要将 的全部内容<div class="row-fluid">..</div>作为单独的 html,但不使用 iframe,我需要一个 div 而不是 iframe,并使其像在一页中一样工作。

采纳答案by multimediaxp

With PHPyou can include a page inside your code inside a specific division.

有了PHP你可以包括你的代码中的特定部门内的内页。

for example inside index.php:

例如在 index.php 中:

<div>
  <?php include('page2.php'); ?>
</div>

and inside page2.php you have:

在 page2.php 里面你有:

<span>Hello World</span>

The result would be:

结果将是:

<div>
  <span>Hello World</span>
</div>

If what you want to achieve needs to be in the front-end as the user navigates through your site; that is after a click is made to an element and you don't want to change to another page, then AJAXis your option. this example is with Jquery:

如果您想要实现的目标需要在用户浏览您的网站时位于前端;也就是在点击一个元素之后你不想切换到另一个页面,那么AJAX是你的选择。这个例子是用 Jquery:

$('.clickme').click(function(){
  $.ajax({
    url:'page2.php'
    success:function(msg){
      $('#insert_div').html(msg)
    }
  });
});

HTML:

HTML:

<span class="clickme">Get Page 2</span>

<div id="insert_div">
  <!-- Page 2 will be inserted here -->
</div>

Another solution is Jquery load()as many have posted:

另一个解决方案是Jquery load()正如许多人发布的那样:

$('.clickme').click(function(){
  $('#insert_div').load("page2.php");
});

回答by Vivek S

You can use the jQuery load()method to load the content when the corresponding link is clicked. you can also use this without event clicked with animation.

您可以使用 jQueryload()方法在单击相应链接时加载内容。您也可以在没有单击动画的事件的情况下使用它。