2014-02-16 55 views
0

我希望能够从嵌入式vimeo视频访问类。我的iframe如下:嵌入式文档的访问元素

HTML

<iframe id="vimeo" src="//player.vimeo.com/video/85509673?portrait=0&amp;badge=0&amp;color=a82a2a" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> 

我想要一个click事件添加到play-button-cell类,像这样:

jQuery的

var embeddedContent = $("#vimeo"); 
console.log(embeddedContent); 
var doc = embeddedContent.contentDocument; // get the inner DOM 
console.log(doc); 
console.log(doc.jQuery(".play-button-cell")); 

doc.jQuery(".play-button-cell").click(function(){ 
    alert('ok!'); 
}); 

,我得到以下控制台中的错误消息:

Uncaught TypeError: Cannot call method 'jQuery' of undefined 

play-button-cell确实存在,因为我可以在播放按钮上“检查元素”时看到它。 如何访问嵌入式内容中的类?这可能吗?

DEMO

回答

1

实在不行,可以,除非:

  1. 您的网页托管在vimeo.com,或
  2. vimeo.com让您的域中执行跨域交互。

这是出于安全原因 - 想想...访问一个人托管的页面。而在IFRAME上放置他们博客的人,使用JavaScript在你的行为上发布Facebook状态 - 这很糟糕,不是吗?

这种安全性措施被称为同源策略,您可以在这里找到更多的信息(出于安全原因):https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript

附:如果你调试了代码,你应该看到$('#vimeo')。contentDocument返回NULL(或者抛出一个异常取决于你使用的浏览器。)