douzhu1188 2011-11-29 16:54
浏览 32
已采纳

jQuery刷新停止jQuery悬停工作

I'm using the script below to refresh a div when the text 'view more' is clicked. The div loads a bunch of random images from the rotate.php file. For some reason though, the jquery hover effects that I have applied no longer work after the div has been refreshed. I did try adding the jquery hover script to the rotate.php file, but that stops the refresh script from working... :S Does anyone have any ideas on how you could fix this? :)

<div class="headingtext">
<script type='text/javascript'>
$(function() {
  $("#refresh").click(function(evt) {
     $(".feature").load("rotate.php")
     evt.preventDefault();
  })
})
</script><a id="refresh" href="#">View More</a>
        </div>

        <div class="feature">
            <?php include('rotate.php') ?>
        </div>

The jquery I have applied to the images, but stops working after refresh:

 $(document).ready(function(){
 $(".feature img").hover(function() {
  $(this).stop().animate({opacity: "0.5"}, 'slow');
},
function() {
  $(this).stop().animate({opacity: "1"}, 'slow');
});
});

The PHP (rotate.php):

<?php 
$random = "random.txt";
$fp = file($random);
shuffle($fp);
$keys = array_rand($fp, 3);
for ($i = 0; $i < 3; $i++):
$rl = $fp[$keys[$i]]; 
echo $rl;
endfor;
?>
  • 写回答

2条回答 默认 最新

  • doulun7739 2011-11-29 16:57
    关注

    You must use the on function ( http://api.jquery.com/on/ )

    replace :

     $(".feature img").hover(function() {
    

    by

    $(".feature img").on("mouseover", function() {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)