iframe中的点击事件 [英] Click event in iframe

查看:1005
本文介绍了iframe中的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据iframe中的当前网址来附加iframe所在父页面的网址.

I'm trying to append the url of the parent page an iframe is located in based on the current url within the iframe.

这都是在同一域上发生的,因此我认为不应该存在任何安全问题.

This is all taking place on the same domain, so I don't think there should be any security issues.

我的代码现在如下所示:

My code now looks like the following:

function locator_url() {

var iframeUrl = alert(document.getElementById("dealer-        locator").documentWindow.location.href);

var iframeUrlSplit = iframeUrl.split('/locator/');

window.location.hash = '#' + iframeUrlSplit[1];

};


$(document).ready(function(){

    document.getElementById("dealer-locator").contentWindow.onload = locator_url(); 
});

现在,iframe的默认src是 http://localhost/meade/locator/ iframe所在的页面是 http://localhost/meade/dealerlocator/

Now the default src for the iframe is http://localhost/meade/locator/ The page the iframe is on is http://localhost/meade/dealerlocator/

该代码适用于初始页面加载,父URL附加到localhost/meade/dealerlocator/#

The code works for the initial page load, the parent url is appended to localhost/meade/dealerlocator/#

但是,当我单击iframe中的链接时,即使iframe中的href值已更改,父网址也不会更改.

However, when I click a link inside the iframe the parent url doesn't change, even though the href value in the iframe has.

父网址应将其哈希值更新为:

The parent url should have it's hash updated to something like:

localhost/meade/dealerlocator/#results_list.php?showonly = US& tab = US& zip =& distance = 10& state =& city =& name =

localhost/meade/dealerlocator/#results_list.php?showonly=US&tab=US&zip=&distance=10&state=&city=&name=

但是那没有发生.

我想念什么?

推荐答案

好吧,如果您需要在iframe中单击事件,则可以通过以下方式做到这一点:

Well, if you need click events in your iframe, then you could do it this way:

<a href="http://www.car.com" onClick="parent.locator_url(); return false;">Car.com</a>

然后在父页面中调用locator_url().使用加载事件也是如此(我的意思是,发生在iframe上下文中的加载事件).

Then locator_url() gets called in the parent page. Same goes for using load events (I mean, load events that occur in the iframe context).

这篇关于iframe中的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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