問(wèn)題是這樣的:在我的主頁(yè) ( parent.html) 中,我有一個(gè) iframe ( child.html) 和一個(gè)腳本塊。在該腳本塊中,有一個(gè)整數(shù)數(shù)組和一個(gè)將元素添加到列表中的函數(shù)。在 iframe 中,有一個(gè)新函數(shù)可以將元素添加到主文件 ( parent.html) 的列表中。我想知道 iframe() 是否可以child.html訪問(wèn)parent.html. 例子:父級(jí).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>錯(cuò)誤:child.html:12 Uncaught ReferenceError: parentList is not defined at addValueInternal (child.html:12) at HTMLButtonElement.onclick (child.html:6)
是否可以訪問(wèn) iframe 外部的數(shù)組?
天涯盡頭無(wú)女友
2023-10-17 19:54:30
