jQuery不能在AJAX加载的div中工作

时间:2015-08-14 08:28:44

标签: php jquery html ajax

这是我的 slider.php 页面,其中 ajax.php 文件的内容是通过ajax加载的。

var xmlhttp;
function showtemplate(temp)
{
    if (window.XMLHttpRequest)
    { 
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState===4 && xmlhttp.status===200) {
                var div = document.getElementById('contain');
                div.innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open('GET', 'ajax.php?section='+temp, true);
        xmlhttp.send();
}

ajax.php 文件的内容正在加载到包含div

这是我的 ajax.php 文件,我使用jquery来幻灯片显示图片。

<?php
    $final=$_REQUEST['section'];
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js?ver=1.4.2"></script>
<script type="text/javascript">
$(function()
{
    var currentIndex = 0,
    var items = $('.container div'),
    var itemAmt = items.length;

    function cycleItems() 
    {
        var item = $('.container div').eq(currentIndex);
        items.hide();
        item.css('display','inline-block');
    }

    var autoSlide = setInterval(function() 
    {
        currentIndex += 1;
        if (currentIndex > itemAmt - 1) 
        {
            currentIndex = 0;
        }   
    cycleItems();
    }, 3000);

    $('.next').click(function() {
        clearInterval(autoSlide);
        currentIndex += 1;
        if (currentIndex > itemAmt - 1) 
        {
            currentIndex = 0;
        }
        cycleItems();
    });

    $('.prev').click(function() {
        clearInterval(autoSlide);
        currentIndex -= 1;
        if (currentIndex < 0) {
            currentIndex = itemAmt - 1;
        }
        cycleItems();
    });
});  
</script>
<section class="demo">
    <button class="next" id="next">Next</button>
    <button class="prev" id="prev">Previous</button>
    <div class="container"> 
        <div style="display: inline-block;">
            <img src='images/<?php echo $final;?>/image1.jpg'/> 
        </div>
    <div>
        <img src='images/<?php echo $final;?>/image2.jpg'/> 
    </div>
    <div>
        <img src='images/<?php echo $final;?>/image3.jpg'/> 
    </div>
    <!--<span u="arrowleft" class="jssora12l" style="width: 41px; height: 50px; top: 110px; left: 2px;"> </span>
    <span u="arrowright" class="jssora12r" style="width: 41px; height: 50px; top: 110px; right: 5px"> </span>-->  
</div>

但是当我点击下一个和上一个按钮时,它不起作用。请帮帮我。

1 个答案:

答案 0 :(得分:0)

我的代码现在正在运行。我只是请求部分并将其显示为拇指而不使用jquery。

<?php

$final=$_REQUEST['section'];

include "slider/relatedcat.php";
?>

<div style="margin-bottom: 10px; font-size: 18px;font-family: 'Roboto Slab',serif;margin-top: -7px">Choose <?php echo ucwords($final);?> Card Template.</div>

<?php 
    for($i=1;$i<=count($template);$i++)
    {           
    ?>
    <div id="thumb<?php echo $i;?>" onclick='showimage("template<?php echo $i; ?>","<?php echo $final;?>");' style='box-shadow: rgba(34, 25, 25, 0.4) 0px 1px 3px;border-radius: 15px;width: 150px !important;height: 150px !important;top: 0px;float:left;margin-right: 20px;margin-bottom:15px;overflow: hidden;transform: perspective(2000px);background: white;cursor: pointer;'>
    <img alt='<?php echo ucwords($final);?>' u='image' src='images/<?php echo $final;?>/template<?php echo $i?>.jpg' style='width:100%;height:100%;' /> 
    </div>
    <?php 
    }       
?>

如果你想在ajax加载的div中使用jquery,你必须在ajax函数成功时重新定义jquery函数,如下面的代码:

xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState===4 && xmlhttp.status===200) {
                var div = document.getElementById('contain2');
                div.innerHTML = xmlhttp.responseText;

                $('#resize').draggable({containment: '#main'}).resizable({maxWidth:350, maxHeight:333});
                $('#msgvisible').draggable({containment: '#main'});
                //alert(xmlhttp.responseText);
                document.getElementById('stylepart').style.display="block";


            }
        }
相关问题