IFRAME 上的点击陷阱
我有一个 iframe,里面有一个来自某处的视频链接。 我想在点击 iframe 时弹出一个 div。
<a href="javascript:alert();" class="iframelink">
<iframe src="http://www.youtube.com/embed/zDNXx-PgU3k" width="213" height="195" frameborder="0"></iframe>
</a>
$('.iframelink').click(function(){
alert('iframe clicked. Open popup.');
})
如何?但因为实际上是另一个页面,javascript 无法捕获点击事件。 有什么办法吗?
I have a an iframe which inside there is a video link from somwhere.
I want popup a div when click on iframe.
<a href="javascript:alert();" class="iframelink">
<iframe src="http://www.youtube.com/embed/zDNXx-PgU3k" width="213" height="195" frameborder="0"></iframe>
</a>
$('.iframelink').click(function(){
alert('iframe clicked. Open popup.');
})
How? But because is actually another page javascript cant catch click event.
Is there any way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您必须将处理程序附加到 iframe 内的链接上,因此您应该在 iframe 内找到它。试试这个。
contents()
参考:http://api.jquery.com/contents/请注意,只有与其父页面位于同一域中的 iframe 资源才可以访问。
Since you have to attach the handler on a link inside an iframe, you should find it inside the iframe. Try this.
contents()
reference: http://api.jquery.com/contents/Note that you can only access iframe resources only if it is in the same domain as its parent page.
除了相当顽皮并在 IFRAME 顶部放置一个不可见的元素(点击劫持)之外,我认为您运气不好,因为我认为 IFRAME 内的事件根本不会冒泡。这里要小心——你正处于危险的境地。那是怪物。
PS 一种选择是让 IFRAME 指向您网站上的一个页面,您可以向该页面添加一个 onclick 事件 - 然后该事件会在父元素上触发一个函数/事件 - 但是您仍然会运气不好我认为当谈到单击 FLASH 对象时会触发。
Apart from being rather naughty and placing an invisible element over the top of your IFRAME (click-jacking), I think you're out of luck as I don't think events within the IFRAME bubble up at all. Careful here - you're in dangerous territory. Thar be monsters.
P.S. one option would be to have the IFRAME point to a page on your site, that you could add an onclick event to - which would then fire a function/event on the parent element - however you're still going to be out of luck when it comes to firing on clicking the FLASH object I think.
您可以尝试利用 YouTube API。
http://code.google.com/apis/youtube/js_api_reference.html#Adding_event_listener
尽管受到限制,您也许可以做您需要做的事情。
You can try to leverage the YouTube API.
http://code.google.com/apis/youtube/js_api_reference.html#Adding_event_listener
Though it is limited you may be able to do what you need to.