gpt4 book ai didi

javascript - 修改 元素的 src 属性后视频未更新

转载 作者:行者123 更新时间:2023-12-03 11:04:18 26 4
gpt4 key购买 nike

我正在尝试更改 <video> 的来源,但它没有播放新视频:

<body onload="setvid()">
<div id="container">
<video width="640" height="360" autoplay loop>
<source id="srcc" src="" type="video/mp4">
</video>
</div>
<script type="text/javascript">
function setvid() {
document.getElementById("srcc").src = "vid" + Math.floor(Math.random() * 10) + ".mp4";
}
</script>
</body>

有什么建议吗?

最佳答案

来自specification (强调我的):

The src attribute gives the address of the media resource. The value must be a valid non-empty URL potentially surrounded by spaces. This attribute must be present.

Note: Dynamically modifying a source element and its attribute when the element is already inserted in a video or audio element will have no effect. To change what is playing, just use the src attribute on the media element directly, possibly making use of the canPlayType() method to pick from amongst available resources. Generally, manipulating source elements manually after the document has been parsed is an unnecessarily complicated approach.

所以看来您应该将新源分配给 <video> 元素代替。

<div id="container">
<video width="640" height="360" autoplay loop />
</div>
<script type="text/javascript">
function setvid() {
document.querySelector('video').src = "...";
}
</script>

关于javascript - 修改 <source> 元素的 src 属性后视频未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27934728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com