如何从图像缩略图源获取YouTube视频ID,并设置为iframe?
因此,如果图像标签是,
<img src="http://i1.ytimg.com/vi/LL0Y4MZ45bo/0.jpg" class="youtubeimg"></img>
我不知道如何从缩略图 src 中提取 YouTube 视频 id (LL0Y4MZ45bo
),然后将其应用到 iframe。
目前我已经 100% 工作,它将 src 应用于 iframe。但我不知道该怎么做,在这种情况下从 img 标签获取 id LL0Y4MZ45bo
并将其添加到 youtube 嵌入 src http://www.youtube.com/embed/LL0Y4MZ45bo
<iframe src="" class="youtubeiframe"></iframe>
<script>
$(".youtubeiframe").attr({
src: "http://www.youtube.com/embed/VIDEO-ID-EXTRACTED-FROM-THUMBNAIL-SRC",
});
</script>
那么我如何从img中提取id并适用于 iframe src?
So if the image tag is
<img src="http://i1.ytimg.com/vi/LL0Y4MZ45bo/0.jpg" class="youtubeimg"></img>
I don't know how to extract the YouTube video id (LL0Y4MZ45bo
) from the thumbnail image src and then apply it to an iframe.
currently i have 100% working, which applys the src to the iframe. but what i dont know how to do is get the id from the img tags in this case LL0Y4MZ45bo
and add it to the youtube embed src http://www.youtube.com/embed/LL0Y4MZ45bo
<iframe src="" class="youtubeiframe"></iframe>
<script>
$(".youtubeiframe").attr({
src: "http://www.youtube.com/embed/VIDEO-ID-EXTRACTED-FROM-THUMBNAIL-SRC",
});
</script>
so how can I extract id from the img and apply to iframe src?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此操作会使用正则表达式提取 YouTube ID,我假设第一个 YouTube 视频至少有 11 个字符ID 为 11。
此方法有效,但是 YouTube 播放器 API可能会为您提供更稳定的解决方案来加载该 iFrame,将来一旦它变得稳定,我会推荐 YouTube iFrame API
This extracts the YouTube ID using a Regular Expression, I made the assumption that it would be a minimum of eleven characters as the first YouTube Video ID has 11.
This works, however the YouTube Player API may provide you with a more stable solution to loading that iFrame, in the future once it becomes stable I would recommend the YouTube iFrame API
我认为,如果您开始研究 YouTube API<,您可以更轻松地做到这一点/a>.
I think you can do this more easily if you'll start to study the YouTube API.
将 URL 作为数组,然后取倒数第二部分。例如,这也适用于 CSS 背景图像。
https://jsfiddle.net/Wobbo/wfyL1hem/7/
Make the URL as an array and than take the second to last part. For example this also works with CSS background-image.
https://jsfiddle.net/Wobbo/wfyL1hem/7/