jQuery 和 Vimeo Froogaloop API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5999357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery and Vimeo Froogaloop API
提问by Drew Baker
I'm building a plugin to WordPress and so far so good with the PHP library. But I am having some issues with the JavaScript API.
我正在为 WordPress 构建一个插件,到目前为止,PHP 库非常好。但是我在 JavaScript API 方面遇到了一些问题。
I'm trying to use it with jQuery, and I think the WordPress version of jQuery is messing with the $f
shortcut. Why wouldn't this work?
我正在尝试将它与 jQuery 一起使用,并且我认为 jQuery 的 WordPress 版本与$f
快捷方式混淆了。为什么这行不通?
var vimeoPlayer = {
init: function() {
var vimeoPlayers = document.querySelectorAll('iframe'),
player;
jQuery('iframe.vimeo-player').each(function(index, iframe){
player = vimeoPlayers[index];
$f(player).vimeoPlayer.addEvent('ready', vimeoPlayer.ready);
});
},
addEvent: function(element, eventName, callback) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
}
else {
element.attachEvent(eventName, callback, false);
}
},
ready: function(player_id) {
alert(player_id);
}
}
jQuery(document).ready(function($){
vimeoPlayer.init.call();
});
You can see it in action at temp.woodshop.tv/?work/?dickies-campaign/?
.
您可以在 中看到它的运行情况temp.woodshop.tv/?work/?dickies-campaign/?
。
I get this error:
我收到此错误:
TypeError: Result of expression '$f(player).vimeoPlayer' [undefined] is not an object.
类型错误:表达式 '$f(player).vimeoPlayer' [undefined] 的结果不是对象。
回答by Jon Kirkman
One issue is that addEvent is both a function that you've defined and also a method of the $f(player) object. It appears that you're confusing the two. The addEvent method of the $f(player) object only takes two arguments, the name of the player event and the function to be called. It should be used as $f(your-iframe).addEvent('vimeo event', your_function);
一个问题是 addEvent 既是您定义的函数,也是 $f(player) 对象的方法。看来你把两者混淆了。$f(player) 对象的 addEvent 方法只接受两个参数,播放器事件的名称和要调用的函数。它应该用作 $f(your-iframe).addEvent('vimeo event', your_function);
Your addEvent function will unify events between IE and W3C methods. It's not needed because you're using jQuery. jQuery(whatever).click() does the same thing. I don't see any part of your snippet where you need it but if you do, I'd just use the jQuery method.
您的 addEvent 函数将统一 IE 和 W3C 方法之间的事件。不需要,因为您使用的是 jQuery。jQuery(whatever).click() 做同样的事情。我没有看到您需要的代码段的任何部分,但如果您这样做了,我将只使用 jQuery 方法。
Also, the video player object should be $f(player) instead of $f(player).vimeoPlayer
此外,视频播放器对象应该是 $f(player) 而不是 $f(player).vimeoPlayer
Try this
尝试这个
jQuery('iframe.vimeo-player').each(function(){
$f(this).addEvent('ready', ready);
});
Another thing to note is that any additional player events need to be added from your ready callback function. For example:
另一件需要注意的是,任何额外的播放器事件都需要从您准备好的回调函数中添加。例如:
function ready(player_id){
$f(player_id).addEvent('play', play);
$f(player_id).api('play');
alert("Ready!!!");
}
function play(){
alert("Playing!!!");
}
I've had a hard time finding the info I want on Vimeo's Froogaloop api but after scouring Vimeo Froogaloop API Playgroundabout a dozen times I'm starting to get the idea of how it works.
我很难在 Vimeo 的 Froogaloop api 上找到我想要的信息,但是在搜索Vimeo Froogaloop API Playground大约十几次之后,我开始了解它是如何工作的。
Good luck!
祝你好运!
回答by seoul
Instead of $f(player).vimeoPlayer.addEvent
try $(this).addEvent
.
而不是$f(player).vimeoPlayer.addEvent
尝试$(this).addEvent
。