2012-02-16 103 views
0

我想编辑下面的代码(该代码被来自[http://stackoverflow.com/a/9280414/945688]如何在查询中插入变量值?

<!DOCTYPE html> 
<html> 
    <head> 
    <title>image</title>   
    </head> 
    <body> 

    <img id="img" src="1.jpg" /> 

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script type="text/javascript"> 
     var interval = null; 
     function changeImage(){ 
     var nextNum = parseInt($('#img').attr('src').replace('.jpg',''), 10) + 1; 
     // If you want it to repeat back to 1 after 5 
     nextNum = nextNum > 5 ? 1 : nextNum; 
     $('#img').attr('src', nextNum+'.jpg'); 
     } 

     $('#img').hover(
     function(){ 
      // Call change Image every 50ms 
      interval = setInterval(changeImage, 50); 
      changeImage(); 
     }, 
     function(){ 
      // Stop the call 
      interval && clearInterval(interval); 
     } 
    ); 
    </script> 
    </body> 
</html>​​​​​​​​​​​​ 

上述代码是细work.but我想在替代图像路径。

如果

<?php $image_folder_path='imagenewfolder';?> 
<img id="img" src="images/<?php echo $image_fold_path; ?>/1.jpg> 

,如何编辑jQuery代码。

+0

我没有得到你的问题,你可以多帮一点吗? – Shaheer 2012-02-16 05:22:44

+0

看到这个链接[链接] http://stackoverflow.com/a/9280414/945688.我想改变php的jQuery代码变量值。 – 2012-02-16 05:33:56

+0

你想改变哪个值? – Shaheer 2012-02-16 05:46:55

回答

1

试试这个代码。

var interval = null; 
var imgPath = '<?php echo $image_path; ?>'; // it should have a trailing slash 
     function changeImage(){ 
     var nextNum = parseInt($('#img').attr('src').replace(imgPath,'').replace('.jpg',''), 10) + 1; 
     // If you want it to repeat back to 1 after 5 
     nextNum = nextNum > 5 ? 1 : nextNum; 
     $('#img').attr('src', imgPath + nextNum+'.jpg'); 
     } 

     $('#img').hover(
     function(){ 
      // Call change Image every 50ms 
      interval = setInterval(changeImage, 50); 
      changeImage(); 
     }, 
     function(){ 
      // Stop the call 
      interval && clearInterval(interval); 
     } 
    ); 
+0

谢谢Shaheer.Now我明白了。 – 2012-02-16 06:17:58

+0

欢迎家伙:) – Shaheer 2012-02-16 06:19:19