亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否可以訪問 iframe 外部的數組?

是否可以訪問 iframe 外部的數組?

天涯盡頭無女友 2023-10-17 19:54:30
問題是這樣的:在我的主頁 ( parent.html) 中,我有一個 iframe ( child.html) 和一個腳本塊。在該腳本塊中,有一個整數數組和一個將元素添加到列表中的函數。在 iframe 中,有一個新函數可以將元素添加到主文件 ( parent.html) 的列表中。我想知道 iframe() 是否可以child.html訪問parent.html. 例子:父級.html<html>    <head>        <title>Parent</title>        <script>            var parentList = [0];            var counter = 0;            function addValue(){                counter++;                parentList.push(counter);                console.log('parent', parentList);            }        </script>    </head>    <body>        <button onclick="addValue()">Add Value (Parent)</button>        <br />        <iframe src="child.html" allowfullscreen></iframe>    </body></html>孩子.html<html>    <head>        <title>Child</title>    </head>    <body>        <button onclick="addValueInternal()">Add Value Child</button>        <script>            var internalCount = 0;            function addValueInternal() {                internalCount++;                parentList.push(internalCount);                console.log('child', parentList);            }        </script>    </body></html>錯誤:child.html:12 Uncaught ReferenceError: parentList is not defined    at addValueInternal (child.html:12)    at HTMLButtonElement.onclick (child.html:6)
查看完整描述

1 回答

?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

是的。有可能的。

因此,在您的情況下,您必須parent在訪問數組時引用 。

function addValueInternal() {

? internalCount++;

? parent.parentList.push(internalCount); // here we access the reference

? console.log('child', parentList);

}

請注意,之后您可能會遇到有關跨域策略的問題。


查看完整回答
反對 回復 2023-10-17
  • 1 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號