我使用Youtube Iframe API和使用他們的腳本玩家加載罰款。但是,當我做一個Ajax調用包含JavaScript代碼的php文件來運行播放器時,它不會執行。Javascript無法在AJAX後
function get_content(song_id,url,song_title) {
if (song_id == null)
return;
document.title = song_title + browser_title_suffix;
$.get('/ajax/player.php',{song_id:song_id}, function(data) {
$('#main_content').html(data).css({'opacity':0}).animate({'opacity':1});
});
}
/ajax/player.php包含此代碼 - https://developers.google.com/youtube/iframe_api_reference#Loading_a_Video_Player
然而,玩家不試。我甚至嘗試過player.remove(),但那也沒有幫助。
更新1: 我的PHP文件:
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
$(document).ready(function(){
get_content('.$row[song_id].',null,"'.$row[title].'");
});
</script>
我的JavaScript文件:
function get_content(song_id,url,song_title) {
if (song_id != null) {
$.get('/ajax/player.php',{song_id:song_id}, function(data) {
video_id = data;
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: video_id,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
});
}
}
但是,視頻播放器不加載。
他在控制檯中的任何東西? PHP加載後是否直接顯示正確的數據? – mplungjan 2013-02-10 09:36:18
不,控制檯中什麼都沒有,當php第一次加載數據時,它一切正常。這是當我想加載新的內容,它不起作用。 – goldenboy48 2013-02-10 10:02:53