我有一个 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无法捕获点击事件。 有什么办法吗?
最佳答案
由于您必须将处理程序附加到 iframe 内的链接上,因此您应该在 iframe 内找到它。试试这个。
$('a.iframelink iframe').contents()//Will get iframe contents
.find('videoLinkSelector')//Pass the required selector
.click(function(){
//Write your code here
});
contents()
引用:http://api.jquery.com/contents/
请注意,只有当它与其父页面位于同一域中时,您才能访问 iframe 资源。
关于javascript - 点击 IFRAME 的陷阱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9066550/