![](/img/trans.png)
[英]HTML5 video get currentTime not working with media events javascript addEventListener
[英]javascript pass media EventListener to html (html5) media.currentTime
*更新为您看到的代码编辑工作) https://codeshare.io/a3ZJ9g
我需要将javascript变量传递给html链接...
我的原始问题是HTML5视频获取currentTime无法与媒体事件一起使用javscript addEventListener
工作代码:
<script>
var media = document.getElementById('myVideo');
// durationchange
var isdurationchange = function(e) {
$("#output").html(media.currentTime);
var x = document.createElement("a");
};
media.addEventListener("timeupdate", isdurationchange, true)
</script>
该代码有效,但我需要它将currentTime值回显到html页面,例如
document.write("<a href=/time.htm?currentTime='.media.addEventListener("timeupdate", isdurationchange, true).'>link</a>;);
所以它会打印出来
<a href=time.htm?currentTime=currenttimefromjavascript>link</a>
谢谢
我确实读过: 如何将javascript变量传递给html标签
有人建议:
// insert the `a` somewhere appropriate so it can be clicked on:
const a = document.body.appendChild(document.createElement("a"));
a.textContent = 'Link to current time';
const isdurationchange = function(e) {
a.href = `\\time.htm?currentTime=${media.currentTime}`;
};
但是那去了哪里?
document.write
尝试写入当前文档。 如果文档已被处理,则该文档将被替换为带有您的参数的空白文档。 你不想要那个 请改用适当的DOM方法。
如果对于要动态创建的元素,要在每次timeupdate
上更改其href
,则应执行以下操作:
// insert the `a` somewhere appropriate so it can be clicked on:
const a = document.body.appendChild(document.createElement("a"));
a.textContent = 'Link to current time';
const isdurationchange = function(e) {
a.href = `\\time.htm?currentTime=${media.currentTime}`;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.