【发布时间】:2016-07-02 05:19:33
【问题描述】:
使用哈希更改视频源仅适用于其中一个标签。
例如mywebsite.com/video.php会显示默认视频,如果我转到mywebsite.com/video.php#video1,它将更改为video-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测试中。 -
你也在使用
=,而应该是==来比较。
标签: javascript html video src