简体   繁体   English

在另一个网页/ IFRAME脚本的div中显示内容

[英]Showing content in a div from another webpage / IFRAME script

I got 10 galleries, let's say gallery1.php, gallery2.php etc. 我有10个美术馆,比方说gallery1.php,gallery2.php等。

and I want to make a navigator, that whenever i press on "First Gallery" or "Secont Gallery" etc. 而且我想制作一个导航器,每当我按“ First Gallery”或“ Secont Gallery”等。

what it will do is only changing a content of a div or something else. 它只会改变div或其他内容。

The galleries shows the images with for loop. 画廊显示带有for循环的图像。 basically, I rename all of the photos to series numbers and then use a for loop to print them one after another. 基本上,我将所有照片重命名为序列号,然后使用for循环将它们依次打印。

I tried using the IFRAME of HTML but when i tried to do a script that will set it's height to the content height, I faced a problem because the script didn't work on all the browsers. 我尝试使用HTML的IFRAME,但是当我尝试执行将其高度设置为内容高度的脚本时,我遇到了一个问题,因为该脚本无法在所有浏览器上正常工作。 I Google for other scrips, and tried about 10 scripts by now, but none seems to work on all the browsers. 我在Google上搜索了其他脚本,到现在为止已经尝试了大约10个脚本,但是似乎没有一个脚本可以在所有浏览器上正常工作。

If you have a script that will work on all browsers, that would help. 如果您拥有可在所有浏览器上使用的脚本,那将会有所帮助。

If you don't, how do I change a div content to content from another webpage? 如果不这样做,如何将div内容更改为另一个网页的内容? Just consider that that content that i'm trying to pull from the other webpage, is a result of a For loop. 只需考虑我试图从其他网页中提取的内容是For循环的结果。

I really don't want to reload the whole page every time I choose another gallery. 我真的不想每次选择另一个画廊都重新加载整个页面。

You can achieve this with AJAX. 您可以使用AJAX来实现。 Basically when you choose a new gallery, the JavaScript (recommended to use jQuery) will fetch the relevant PHP page and put it into the div, like so: 基本上,当您选择一个新的库时,JavaScript(建议使用jQuery)将获取相关的PHP页面并将其放入div中,如下所示:

$.post('gallery1.php', function(data) {
  $('#the-div-id').html(data);
});

You can use AJAX to get content from server and insert into a page without reloading the whole page. 您可以使用AJAX从服务器获取内容并将其插入页面,而无需重新加载整个页面。

jQuery has numerous AJAX methods of which the simplest is load() . jQuery有许多AJAX方法,其中最简单的就是load()

Give your gallery links a common class: 给您的画廊链接一个普通的类:

<a class="gallery_link" href="gallery1.php">One</a>

Then in jQuery: 然后在jQuery中:

$(function(){    
    $('.gallery_link').click(function(){
      /* "this" inside click handler refers to link clicked*/
     var galleryUrl= this.href;
      $('#galleryDiv').load(galleryUrl, function(){
        /* new content is oaded into the DIV, can run any other code here that manages new content*/
      });
      /* prevent browser opening url in window*/
      return false;    
    })
});

API refernce: http://api.jquery.com/load/ API参考: http//api.jquery.com/load/

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

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