【问题标题】:using hash to change video src on a page使用哈希更改页面上的视频 src
【发布时间】: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


【解决方案1】:

您使用的是=,而它应该是==。即使您删除了hash 开头的#,您仍然将它包含在比较字符串中。

if (hash == 'video1') {
  document.getElementById('myVideo') .src=a = "http://mywebsite.com/videos/video-1.mp4";
} else if (hash == 'video2') {
  document.getElementById('myVideo') .src=a = "http://mywebsite.com/videos/video-2.mp4"";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多