0

我之前创建了这个问题,但以另一种方式,没有得到答案。所以今天我写了一些简单的代码来清晰地分享我的问题。

  1. 我曾经jQuery调用图像幻灯片功能。
  2. 中的 AJAX 函数show.php将调用get.php并在 DIV 中打印结果。

我的问题是在提供的 DIV 内滑动(上一个 - 下一个)get.phpshow.php. 但是如果我get.php直接在我的浏览器中调用,那么它就可以工作。

我很困惑,我想在调用 AJAX 时我的 div 中有错误。

我的文件

显示.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>test </title>
        <link rel="stylesheet" type="text/css" href="demo.css" />
        <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="newscript.js"></script>
        <link href="themes/2/js-image-slider.css" rel="stylesheet" type="text/css" />
        <script src="themes/2/js-image-slider.js" type="text/javascript"></script>
        <link href="generic.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <?php
            include("samiloxide.php");
            $sql=mysql_query(" select * from  section ");

            while($r=mysql_fetch_array($sql)){
                echo "<li><a  onclick='loadpage($r[id])' >$r[section]</a></li>" ;
            }
        ?>
        <div id="pageContent"></div>
    </body>
</html>

新脚本.js

var section;
function loadpage(section){ 
    var section = section.toString();

    $.ajax({
       type: "POST",
       url: "get.php",
      dataType: "script",
       data: ({section : section}),
     success: function(html){
            $("#pageContent").empty();
            $("#pageContent").append(html);
        }
    });
}

获取.php

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<style type="text/css">
    <!--
    #gallery-wrap{margin: 0 auto; overflow: hidden; width: 732px; position: relative;}
    #gallery{position: relative; left: 0; top: 0;}
    #gallery li{float: left; margin: 0 20px 15px 0;}
    #gallery li a img{border: 4px solid #40331b; height: 175px; width: 160px;}
    #gallery-controls{margin: 0 auto; width: 732px;}
    #gallery-prev{float: left;}
    #gallery-next{float: right;}
    -->
</style>

<script type="text/javascript">
    <!--
    $(document).ready(function(){ 
        // Gallery
        if(jQuery("#gallery").length){
            // Declare variables
            var totalImages = jQuery("#gallery > li").length, 
            imageWidth = jQuery("#gallery > li:first").outerWidth(true),
            totalWidth = imageWidth * totalImages,
            visibleImages = Math.round(jQuery("#gallery-wrap").width() / imageWidth),
            visibleWidth = visibleImages * imageWidth,
            stopPosition = (visibleWidth - totalWidth);

            jQuery("#gallery").width(totalWidth);

            jQuery("#gallery-prev").click(function(){
                if(jQuery("#gallery").position().left < 0 && !jQuery("#gallery").is(":animated")){
                    jQuery("#gallery").animate({left : "+=" + imageWidth + "px"});
                }
                return false;
            });

            jQuery("#gallery-next").click(function(){
                if(jQuery("#gallery").position().left > stopPosition && !jQuery("#gallery").is(":animated")){
                    jQuery("#gallery").animate({left : "-=" + imageWidth + "px"});
                }
                return false;
            });
        }
    });
    -->
</script>

           <?php
            include("samiloxide.php");
//if(!$_POST['page']) die("0");

$section = (int)$_POST['section'];

$sql=mysql_query(" select * from images  where section='$section'");

echo "
<div id='gallery-wrap'>
    <ul id='gallery'>
    ";


while($rr=mysql_fetch_array($sql)){

      echo " <li><a href='$rr[image]'><img src='$rr[image]' alt='' /></a></li>";
           }
           echo "

    </ul>
</div>
<div id='gallery-controls'>
    <a href='#' id='gallery-prev'><img src='images/prev.png' alt='' />next</a>
    <a href='#' id='gallery-next'><img src='images/next.png' alt='' />last</a>
</div>
           ";
?>
4

1 回答 1

0

这有点复杂,我无法为您的代码提供快速修复,您只需复制、粘贴和验证即可。

get.php中,您加载带有图库的文档,并将设置为工作图库$(document).ready()

但是在show.php,当你加载get.php文件时,$(document).ready()不会被调用。$(document).ready()of很久以前就已经被调用了show.php,并且您的文档现在处于该interactive状态。因此,当您加载布局时,您不会自动执行使该布局工作的代码。

您必须将$(document).ready()代码移动到 in 中get.phpshow.php然后将其绑定到 AJAX 调用完成。或者解get.php$(document).ready().

但是,这并不能在所有浏览器中一直得到保证,因为虽然$(document).ready()在文档准备好时会正确调用,但show.php您所做的是要求加载 HTML 文件。

HTML 被加载,因此onLoad被触发。你不能指望不同。

然后该 HTML 要求加载其他资产(例如图像),但浏览器不知道这一点。它已经触发了onLoad,所以你已经执行了画廊设置代码。如果布局需要已经加载图像的 SRC 才能正确设置样式,那么它并不总是有效。它可能会第二次起作用,因为图像位于浏览器缓存中。它可能适用于快速连接,而不适用于慢速连接;它可能适用于快速加载的小图像,而不适用于较大的图像。所有这些行为表明已经加载的图像是必要的

Again, a quick and dirty fix is to fire the setup after a suitable delay (but what is suitable? You can't know). Another possibility, if all the images are of known sizes, is to supply those sizes in HTML or CSS. After all, the layout usually requires images being loaded so that they occupy space on the page, but for that, you don't need images to be actually displayable. They might be empty spaces (maybe styled with a background).

A third possibility, more complicated but guaranteed to work in all browsers, is to save the image SRC's into another kind of tag (e.g. DIVs with a class of imageloading, by default hidden), and after the load() success to analyze these tags and convert them to IMGs attaching an onload to them. When all those onloads have fired, you know that it's OK to launch the gallery setup. While longer to describe (and code), this last method is actually much faster than the naive "quick fix: wait a bit and fire setup" one.

于 2012-11-22T09:54:09.903 回答