IFRAME 上的点击陷阱

发布于 2024-12-29 17:00:44 字数 441 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归属感 2025-01-05 17:00:44

由于您必须将处理程序附加到 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 资源才可以访问。

Since you have to attach the handler on a link inside an iframe, you should find it inside the iframe. Try this.

$('a.iframelink iframe').contents()//Will get iframe contents
.find('videoLinkSelector')//Pass the required selector
.click(function(){
   //Write your code here
});

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.

断爱 2025-01-05 17:00:44

除了相当顽皮并在 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.

桃扇骨 2025-01-05 17:00:44

您可以尝试利用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文