2012-02-16 24 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