简体   繁体   English

通过使用jQuery在iframe div中单击来在iframe外部重新加载div内容

[英]Reload div content outside an iframe by clicking inside an iframe div, with jquery

I have an mainpage, we will call it "main.php". 我有一个主页,我们将其称为“ main.php”。 At this page exists an iframe, we will call it "frameX". 在此页面上存在一个iframe,我们将其称为“ frameX”。

1. Example html of "main.php" 1.“ main.php”的示例html

<html>
<div class="result">20</div>
<div class="result">25</div>
<div class="result">154</div>
<iframe id="X"></iframe>
</html>

2. Example html of "frameX": 2.“ frameX”的示例html:

<div class="button"></div>

The content of the "frameX" and the "main.php" are from the same URL (like: www.example.com) “ frameX”和“ main.php”的内容来自同一URL(例如:www.example.com)

Now the question: How can I reload all "result" divs (outside the iframe) by clicking at the "button" div (inside the iframe)? 现在的问题是:如何通过单击“按钮” div(在iframe中)重新加载所有“结果” div(在iframe之外)?

I have tried the jQuery "load" function: 我已经尝试过jQuery的“加载”功能:

$( '.button' ).click(function() {
    $(".result").load("https://www.example.com/main.php" + ".result");
});

... but this works for me only at the same page. ...但这仅对同一页面有效。 Has anybody a simple solution? 有没有人简单的解决方案? Is it even possible? 可能吗?

You need to indicate the context where jQuery will select the elements. 您需要指出jQuery选择元素的上下文。

$( '.button' ).click(function() {
    $(".result", window.parent.document).load("https://www.example.com/main.php" + ".result");
});

As far as I know there is no onClick event for an iframe , but you can hook to the actual document inside the iframe. 据我所知,没有iframe onClick事件,但是您可以在iframe中连接到实际文档。

document.getElementById("iframe-id").contentWindow.document.body.onclick = 
function() {
  $(".result").load(https://www.example.com/main.php + ".result");
}

Note: this will only work if the iframe is rendering a page within the same domain. 注意:仅当iframe在同一域内呈现页面时,此方法才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM