javascript 将媒体 EventListener 传递给 html (html5) media.currentTime

javascript pass media EventListener to html (html5) media.currentTime

*更新这里是我的代码编辑,如你所见)https://codeshare.io/a3ZJ9g

我需要将 javascript 变量传递给 html link...

我原来的问题在这里

工作代码:

  <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>

谢谢

我读过: how to pass javascript variable to html tag

How can I pass value from javascript to 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}`;
};