0
使用散列更改視頻源僅適用於其中一個標籤。使用散列來更改頁面上的視頻src
例如mywebsite.com/video.php
會顯示默認的視頻,如果我去mywebsite.com/video.php#video1
它會改變視頻-1,但如果我去mywebsite.com/video.php#video2
,它仍然顯示視頻1.
<script>
// test all possible places hash could be on different browsers
if(window.location.hash){
hash = window.location.hash;
} else if (document.location.hash){
hash = document.location.hash;
} else if (location.hash){
hash = location.hash;
}
// some browsers start the hash with #, remove it for consistency
if(hash.substring(0,1) == '#'){
hash = hash.substring(1,hash.length);
}
if(hash = '#video1'){
document.getElementById('myVideo') .src=a = "http://mywebsite.com/videos/video-1.mp4";
}
if(hash = '#video2'){
document.getElementById('myVideo') .src=a = "http://mywebsite.com/videos/video-2.mp4"";
}
</script>
<video id="myVideo" controls>
<source src="http://mywebsite.com/videos/video-default.mp4">
</video>
我該如何解決這個問題?有沒有更好的方法來改變使用某種類型的ID的src路徑?
您在'hash'開頭刪除了'#',然後將它包含在'if'測試中。 – Barmar
當你應該用==來比較時,你也使用'='。 – Barmar