Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用iframe中的父窗体捕获单击_Javascript_Jquery_Html_Iframe - Fatal编程技术网

Javascript 使用iframe中的父窗体捕获单击

Javascript 使用iframe中的父窗体捕获单击,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我有一个带有iframe的父页面。iframe中有一个标记。在父页面上,我可以在按下该标记时触发事件吗?我知道,如果我将捕获事件的javascript放在iframe中,它就会工作,但是对于我的项目,我需要让父页面捕获它。 这是到目前为止我的代码。 父代码: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type

我有一个带有iframe的父页面。iframe中有一个标记。在父页面上,我可以在按下该标记时触发事件吗?我知道,如果我将捕获事件的javascript放在iframe中,它就会工作,但是对于我的项目,我需要让父页面捕获它。 这是到目前为止我的代码。 父代码:

    <html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
 <script type="text/javascript">
 var $MyFrame= $("#myiframe");
 $MyFrame.on("click", "a", function (e) {
         alert("hello");
     });
</script>
</head>
<body>
<iframe id="myiframe" height="3500" width="950" src="Iframe page" frameborder="0" style="display: block; margin: 10px auto;"></iframe>
</body>
</html>

var$MyFrame=$(“#myiframe”);
$MyFrame.on(“单击”,“a”,函数(e){
警惕(“你好”);
});
这是iframe中的我的页面:

<html>
<head>
</head>
<body>
<a href="www.google.com">Click Me!</a>
</body>
</html>


这可能吗?

不确定,但如果帧源位于同一原点,这可以帮助您处理
.contents()

 var $MyFrame= $("#myiframe");
 $MyFrame.contents().find('a').on("click", function (e) {
     alert("hello");
 });

我注意到您分配了一个
id
,并且您正在使用
class
引用该框架。您的
myiframe
id
而不是
class
。。使用id选择器
#

试试这个

var $MyFrame= $("#myiframe");
$MyFrame.on("click", "a", function (e) {
     alert("hello");
});
var $MyFrame = $("#myiframe");

// You need to wait for the iFrame content to load first
// So, that the click events work properly
$MyFrame.load(function () {
    var frameBody = $MyFrame.contents().find('body');
    frameBody.find('a').click(function () {
        // here is the iframe a click callback
        alert("hello");
    });
});