我试图了解如何使用 YouTube API 定位现有的 iframe(即无需使用脚本构建 iframe)。
与往常一样,Google 没有提供足够的 API 示例,但解释说这是可能的,此处 http://code.google .com/apis/youtube/iframe_api_reference.html
这是我正在尝试执行的示例 - 缩略图下方的视频应该播放。我快到了,但只播放第一个视频...
http://jsfiddle.net/SparrwHawk/KtbYR/2/< /a>
I'm trying to understand how to target an existing iframe using the YouTube API (i.e. without constructing an iframe with the script).
As usual, Google does not give enough API examples, but explains that it IS possible, here http://code.google.com/apis/youtube/iframe_api_reference.html
Here is an example of what I'm trying to do - the video underneath the thumbnail should play. I am almost there, but only the first video plays...
http://jsfiddle.net/SparrwHawk/KtbYR/2/
发布评论
评论(1)
TL;DR:演示:http://jsfiddle.net/KtbYR/5/
YT_ready
、getFrameID
和onYouTubePlayerAPIReady
是 这个答案。这两种方法都可以在没有任何预加载库的情况下实现。在我之前的回答中,我展示了一种为单帧实现该功能的方法。在这个答案中,我重点关注多个框架。
HTML 示例代码(重要标签和属性均大写,
):
JavaScript 代码 (
YT_ready
,getFrameID
、onYouTubePlayerAPIReady
和 YouTube Frame API 脚本加载器定义为 此处)在我之前的回答中,我绑定了
onStateChange
事件。在此示例中,我使用了onReady
事件,因为您只想在初始化时调用这些函数一次。此示例的工作原理如下:
以下方法在此中定义回答。
onYoutubePlayerAPIReady
,进而激活使用YT_ready
定义的所有函数此处显示以下方法的声明,但使用这个实现回答。基于示例的说明:
对象,该对象位于
<.. class="thumb">
之后。id
并将其存储在identifier
变量中。getFrameID
检索。这可确保针对 API 进行正确格式化。 在此示例代码中,返回的 ID 等于
identifier
,因为我已经将 ID 附加到。李>
存在且有效的 YouTube 视频时,会创建一个新的播放器实例,并且引用存储在
players
对象中,可通过键frameID
。onReady
* 事件。当框架的 API 完全初始化时,将调用此方法。createYTEvent
此方法返回一个动态创建的函数,该函数为单独的玩家添加功能。代码中最相关的部分是:
frameID
是框架的 ID,用于启用 YouTube Frame API。identifier
是YT_ready
中定义的 ID,不一定是元素。
getFrameID
将尝试查找给定 id 最接近的匹配帧。也就是说,它返回给定元素的 ID,或者:如果给定元素不是
,则该函数查找子元素这是一个
,并返回该框架的 ID。如果框架不存在,该函数将在给定的 ID 后添加
-frame
。请确保您还检查此答案< /a>,因为这个答案的核心功能就是基于此。
其他 YouTube Frame API 答案。在这些答案中,我展示了 YouTube Frame/JavaScript API 的各种实现。
TL;DR: DEMO: http://jsfiddle.net/KtbYR/5/
YT_ready
,getFrameID
andonYouTubePlayerAPIReady
are functions as defined in this answer. Both methods can be implemented without any preloaded library. In my previous answer, I showed a method to implement the feature for a single frame.In this answer, I focus on multiple frames.
HTML example code (important tags and attributes are capitalized,
<iframe src id>
):JavaScript code (
YT_ready
,getFrameID
,onYouTubePlayerAPIReady
and the YouTube Frame API script loader are defined here)In my previous answer, I bound the
onStateChange
event. In this example, I used theonReady
event, because you want to call the functions only once, at initialization.This example works as follows:
The following methods are defined in this answer.
onYoutubePlayerAPIReady
is called, which in his turn activates all functions as defined usingYT_ready
The declaration of the following methods are shown here, but implemented using this answer. Explanation based on the example:
<iframe id>
object, which is placed right after<.. class="thumb">
.id
is retrieved, and stored in theidentifier
variable.getFrameID
. This ensures that the<iframe>
is properly formatted for the API. In this example code, the returned ID is equal toidentifier
, because I have already attached an ID to the<iframe>
.<iframe>
exists, and a valid YouTube video, a new player instance is created, and the reference is stored in theplayers
object, accessible by keyframeID
.onReady
* event is defined. This method will be invoked when the API is fully initialized for the frame.createYTEvent
This method returns a dynamically created function, which adds functionality for separate players. The most relevant parts of the code are:
frameID
is the ID of the frame, used to enable the YouTube Frame API.identifier
is the ID as defined inYT_ready
, not necessarily an<iframe>
element.getFrameID
will attempt to find the closest matching frame for a given id. That is, it returns the ID of a given<iframe>
element, or: If the given element is not an<iframe>
, the function looks for a child which is a<iframe>
, and returns the ID of this frame. If the frame does not exists, the function will postfix the given ID by-frame
.Make sure that you also check this answer, because the core functionality of this answer is based on that.
Other YouTube Frame API answers. In these answers, I showed various implementations of the YouTube Frame/JavaScript API.