点击iframe内部后,通过jQuery或javascript获取提醒 [英] Get alert after click inside iframe, by jQuery Or javascript
问题描述
我想点击iframe内部获取警报确定后,我尝试如下代码但它不起作用。我该怎么办?
I want after click inside iframe get alert ok, i tried as following code but it don't work. what do i do?
我无法更改iframe,它是固定的。
I can not change the iframe, It is fixed.
HTML:
<div class="addv">
<iframe src="http://ad.anetwork.ir/showad/c.php?adwidth=300&adheight=250&aduser=1423687058" height="250" width="300" frameborder="0" scrolling="no" style="background: #FFF url(http://static-cdn.anetwork.ir/img/loader.gif) no-repeat center;"></iframe>
</div>
Jquery:
$(".addv").on("click", "a", function(event) {
alert('ok'); //should alert ok
});
DEMO: http://jsfiddle.net/dky6h1ev/
推荐答案
如果我没弄错,你试图将事件处理程序绑定到 a
标记,该标记已作为 iframe的DOM的一部分呈现
。不幸的是,鉴于DOM来自不同的域,这是不可能的。
If I'm not mistaken, you're attempting to bind an event handler to an a
tag that has been rendered as part of the DOM of the iframe
. Unfortunately, this isn't possible given the fact the DOM has originated from a different domain.
你需要一定程度的访问来操纵子iframe的DOM,一个级别访问同源策略禁止您:
You need a certain level of access to manipulate the child iframe's DOM, a level of access the same-origin policy prohibits you from:
同源策略限制从
加载的文档或脚本的方式一个来源可以与来自另一个来源的资源进行交互。
同源策略用于防止某些
跨站点请求伪造攻击。
The same-origin policy restricts how a document or script loaded from one origin can interact with a resource from another origin. Same-origin Policy is used as a means to prevent some of the Cross-site Request Forgery attacks.
https://developer.mozilla.org/en-US / docs / Web / Security / Same-origin_policy
尝试解决此问题有时令人讨厌,但这对于安全目的至关重要。
It's a nuisance at times to try and work around this, but it's vital for security purposes.
如果iframe在同一个域上,你有选择权 - 但我假设你的绝对URL(至少在你的小提琴中是这种情况),它是在不同的域名。
If the iframe is on the same domain, you have options - but I'm assuming from your absolute URL (and it's at least the case in your fiddle), that it's on a different domain.
这篇关于点击iframe内部后,通过jQuery或javascript获取提醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!