如何使用jquery检测iframe中的click事件

时间:2016-05-24 03:08:58

标签: javascript jquery html iframe

这是我的iframe:

#htmlDiv {
    position: absolute;
    top: 0px;
    left: 300px;
    height: 650px;
    width: 1130px;
}

#htmlFrame {
    position: absolute;
    top: 0px;
    left: 0px;
    margin: 10px 10px 10px 10px;
    padding: 0px 0px 0px 0px;
    height: 630px;
    width: 1110px;
}

<div id="htmlDiv">
    <iframe id="htmlFrame"></iframe>
</div>

$('#htmlFrame').click(function(){
    alert('clicked');
});

但是当我在iframe中点击时,它没有显示&#34;点击&#34;。
如何检测iframe内的点击。

3 个答案:

答案 0 :(得分:4)

您无法在iframe上检测到点击事件。可以做的是,您可以在overlapping layer之上放置iframe并捕获重叠div的点击事件。见下面的代码:

&#13;
&#13;
$("#overlap").on("click",function(){
		alert("click");
		$("#overlap").css("z-index",-1);

});
$("#htmlFrame").on("mouseleave",function(){
		$("#overlap").css("z-index",1);
});
&#13;
#htmlDiv {
    position: absolute;
    top: 0px;
    left: 300px;
    height: 65px;
    width: 113px;
}

#htmlFrame {
    position: absolute;
    top: 0px;
    left: 0px;
    margin: 10px 10px 10px 10px;
    padding: 0px 0px 0px 0px;
    height: 63px;
    width: 110px;
}

#overlap{
  position: absolute;
  background-color:trasparent;
  top: 0px;
  left: 0px;
  margin: 10px 10px 10px 10px;
  padding: 0px 0px 0px 0px;
  width: 110px;
  height:63px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="htmlDiv">
    <iframe id="htmlFrame"></iframe>
  <div id="overlap">
</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您当前的代码工作正常,请尝试将其点击到iframe的边框,系统会显示警告。我尝试通过向iframe

添加叠加层来为您创建解决方案

您可以看到DEMO

<div id="htmlDiv">
    <iframe id="htmlFrame"></iframe>
    <div id="overlay">
    </div>
</div>

答案 2 :(得分:0)

我这样做了:

<div style="position:relative;">
    <iframe src="..."></iframe>
    <div style="position:absolute;top:0;left:0;right:0;bottom:0;eventual mouse cursor style" onclick="eventual alternative action"></div>
</div>