2013-03-13 95 views
1

我有這個腳本,我想播放名稱爲'pr_id'的歌曲,我該如何將它寫入路徑中?謝謝!如何在路徑中傳遞變量

 $(document).ready(function(){ 
      var pr_id = document.getElementById('id') 
      $("#jquery_jplayer_1").jPlayer({ 
       ready: function(){ 
        $(this).jPlayer("setMedia", { 
         mp3:"/sounds/[pr_id].mp3" 
         }); 
         }, 
         swfPath: "js", 
         supplied: "mp3", 
         wmode: "window" 
        }); 
       }); 
+0

你要找的術語是「字符串連接」。 – meagar 2013-03-13 13:22:38

回答

3

像這樣:

$(document).ready(function() { 
    var pr_id = document.getElementById('id'); 

    $("#jquery_jplayer_1").jPlayer({ 
     ready: function() { 
      $(this).jPlayer("setMedia", { 
       mp3:"/sounds/" + pr_id + ".mp3" 
      }); 
     }, 
     swfPath: "js", 
     supplied: "mp3", 
     wmode: "window" 
    }); 
}); 

將字符串與變量:),不幸的是,不像一些其他語言就不是那麼容易包含在字符串變量,你必須拆分字符串起來有點連接。

0
mp3:"/sounds/"+pr_id+".mp3" 

你可以串連它