在用户点击时通过 jQuery 加载 YouTube 视频的最有效方法是什么?
我正在尝试确定使用 jQuery 在用户单击时将视频加载到视频中的最有效方法是什么。
为了提供更多背景信息,我将在 YouTube 上制作大约 30 个剪辑,每个剪辑的时长为 30-60 秒,我希望在用户浏览主题时将它们动态加载到页面右侧的 div 中,并且左侧可能有视频剪辑。
现在,我已经设置了 HTML 和 jQuery。它有效,但我很好奇是否有更好的方法:
<div class="wrapper">
<div class="details_left">
<div class="cluster">
<a href="#" id="johnk" class="vid_trigger"><div class="title">The importance of demonstrating intellectual curiosity</div></a>
<div class="role">John K: Summer Consultant, BCG</div>
<div class="summary">Discussion on how curiousity is important to show in interviews because it leads to better answers on the job</div>
</div>
</div>
<div class="details_right" id="video_container">
video
</div>
</div>
和 jQuery:
$('#johnk').click( function(){
$('#video_container').html('<iframe width="425" height="349" src="http://www.youtube.com/embed/bMvRdr-mUOU?rel=0" frameborder="0" allowfullscreen </iframe>');
})
为了减少每个视频的 .click() 手动编码,我正在考虑创建一个具有 ids-> 的关联数组。嵌入代码。有没有更好的方法来更有效地完成相同的功能?
预先感谢您的任何见解!
I'm trying to determine what is the most efficient way to load videos into a on a user click using jQuery.
To give more context behind this, I'll have about 30 clips on YouTube that are each between 30-60seconds and I'd like to dynamically load them into a div on the right hand side of the page as the user browses the topics and potential video clips on the left hand side.
Right now, I've setup this HTML and jQuery. It works but I'm curious if there is a better method:
<div class="wrapper">
<div class="details_left">
<div class="cluster">
<a href="#" id="johnk" class="vid_trigger"><div class="title">The importance of demonstrating intellectual curiosity</div></a>
<div class="role">John K: Summer Consultant, BCG</div>
<div class="summary">Discussion on how curiousity is important to show in interviews because it leads to better answers on the job</div>
</div>
</div>
<div class="details_right" id="video_container">
video
</div>
</div>
And the jQuery:
$('#johnk').click( function(){
$('#video_container').html('<iframe width="425" height="349" src="http://www.youtube.com/embed/bMvRdr-mUOU?rel=0" frameborder="0" allowfullscreen </iframe>');
})
To reduce hand coding a .click() for each video I am considering creating an associative array that has the ids-> embed code. Is there a better way to accomplish the same functionality more efficiently?
Thanks in advance for any insights!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将 URL 添加到您的 Href 并在调用中获取它,例如:
在您的 HTML 中:
现在在您的 JQuery 中:
you can add the URL to your Href and get it in the call Something Like:
In your HTML:
Now in your JQuery:
您还可以尝试让 jQuery 更改 iframe src 属性。
您可能会在使用 Internet Explorer 中的 iframe 时遇到问题。
此页面描述了该问题:http://osdir.com/ml /youtube-api-gdata/2011-03/msg00369.html
如果您可以放入条件脚本,在 Internet Explorer 中使用 Flash 嵌入而不是 iframe,那么这可能是最好的方法。
You could also try to have jQuery change the iframe src property.
You might run into trouble with the iframe in Internet Explorer.
This page describes the problem: http://osdir.com/ml/youtube-api-gdata/2011-03/msg00369.html
If you can put in conditional scripting that will use the flash embed instead of the iframe if it is in Internet Explorer, that might be the best way to do it.
使用Colorbox
Use Colorbox