用js传递a标签的自定义属性
程序员文章站
2024-02-04 20:10:52
...
将a
标签中的自定义属性videolink
的值传给video
标签的src
属性
<a href="#video0" class="wiggle" videolink="this is a videolink"><img src="../img/bv3.png"></a>
点击a标签跳转到video0
<div class="lightbox" id="video0">
<video class="mid" src="###" controls="controls" id="video">您的浏览器不支持查看该视频。</video>
</div>
js如下
<script>
var list = document.getElementsByTagName("a")
var video = document.getElementById('video')
for (var i = 0; i < list.length; i++) {
list[i].onclick = function() {
//获取a标签中自定义属性videolink的值
var link = this.getAttribute("videolink");
//将videolink的值传给video的src属性
video.setAttribute("src", link);
};
}
</script>