如何将函数绑定到iframe子项上的单击

时间:2013-03-29 11:55:53

标签: javascript jquery iframe

我需要hide div iframe iframes上点击

<a href="#" class="share">Share +</a> - &gt;这是我想在点击上执行某些功能的孩子。

我使用了this answers JQuery code,但它似乎无效。

HTML

<iframe id="map_frame" frameborder="0" src="http://a.tiles.mapbox.com/v3/vucko.map-1p4ens5h.html#17/45.80611110816038/15.924351969962727"></iframe>
<div>on "share +" click this div must hide so the share box goes above the div</div>

JQuery的

$('#map_frame').contents().find('.share').click(function() {
    alert('click');
});

Working JSFiddle

如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

由于iframe文档和根文档来自不同的域,因此无法完成此操作。浏览器不允许这样做,抛出安全例外。

例如,在chrome中,这会导致:

  

不安全的JavaScript尝试使用URL访问框架   http://a.tiles.mapbox.com/v3/vucko.map-1p4ens5h.html#17/45.80611110816038/15.924351969962727   来自带有网址http://fiddle.jshell.net/94Dge/1/show/的框架。域,   协议和端口必须匹配。