簡體   English   中英

通過使用jQuery在iframe div中單擊來在iframe外部重新加載div內容

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

我有一個主頁,我們將其稱為“ main.php”。 在此頁面上存在一個iframe,我們將其稱為“ frameX”。

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.“ frameX”的示例html:

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

“ frameX”和“ main.php”的內容來自同一URL(例如:www.example.com)

現在的問題是:如何通過單擊“按鈕” div(在iframe中)重新加載所有“結果” div(在iframe之外)?

我已經嘗試過jQuery的“加載”功能:

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

...但這僅對同一頁面有效。 有沒有人簡單的解決方案? 可能嗎?

您需要指出jQuery選擇元素的上下文。

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

據我所知,沒有iframe onClick事件,但是您可以在iframe中連接到實際文檔。

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

注意:僅當iframe在同一域內呈現頁面時,此方法才有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM