如何在iframe中获取点击事件触发父框架中的警报 [英] How to get a click event in an iframe trigger an alert in parent frame

查看:135
本文介绍了如何在iframe中获取点击事件触发父框架中的警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有多个嵌入式iframes,并且我正在ifram e内寻找click事件以触发父框架上的警报. (iframe和父框架都是相同的域).

I have several embedded iframes on my page and I'm looking for a click event inside the iframe to trigger an alert on the parent frame. (both iframe & parent frame are same domain).

iFrame HTML元素(iframe具有class="iframe1"类:

iFrame HTML element (the iframe has a class of class="iframe1":

<a class="button" href="#">Trigger Parent Frame Alert</a>

iFrame jQuery:

iFrame jQuery:

$(document).ready(function() {  
  $('.button').click(function() {
    window.parent.triggerAlert();
  });
});

父框架jQuery:

$(document).ready(function() {
function triggerAlert(){
    alert('iFrame button has been clicked');
  }
});

我认为我的父框架功能不完整-不能完全确定iframe是否可以与父DOM通信吗?

I'm thinking my parent frame function is incomplete - and not entirely sure the iframe can communicate with the parent DOM?

我以前在父框架中没有运气的情况下尝试过这种替代方法:

I have previously tried this alternative with no luck in the parent frame:

$('.iframe1').contents().find('.button').click(function () {
  alert("iFrame button has been clicked");
});

推荐答案

为了使您的函数对iframe可见,您需要将其声明为window对象的成员:

In order to make your function visible to the iframe you need to declare it as a member of the window object:

$(document).ready(function() {
    window.triggerAlert = function (){
        alert('iFrame button has been clicked');
    }
});

这篇关于如何在iframe中获取点击事件触发父框架中的警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆