4
我試圖從img中提取YouTube視頻ID,然後將其傳遞到iframe中以播放嵌入的視頻。我的iframe是在一個div內,直到img被點擊時纔會隱藏:更改JavaScript不工作的iframe src
<div id="testimonialbackground" style="display:none;">
<a href="#" onclick="toggledisplay(this);">Click here</a> to close the video
<iframe id="testimonialframe" src="" frameborder="0" allowfullscreen></iframe>
</div>
<img src="http://img.youtube.com/vi/x-ROeKGEYSk/1.jpg" onclick="toggledisplay(this);" />
這裏是JavaScript。它正常工作,直到這條線elem.setAttribute('src', newsrc);
,此時我的頁面死機:
function toggledisplay(obj) {
var div = document.getElementById('testimonialbackground');
var elem = document.getElementById('testimonialframe');
if (div.style.display == "block") {
div.style.display = "none";
elem.setAttribute('src', "");
}
else {
div.style.display = "block";
var explosion = obj.src.split("/");
var newsrc = "http://www.youtube.com/embed/" + explosion[4] + "?autoplay=1";
elem.setAttribute('src', newsrc); // <---- BROKEN LINE RIGHT HERE
elem.contentWindow.location.reload(); //to refresh the iframe
}
}
感謝您的幫助!