2
iframe有問題。如何從iframe中的文檔對象獲取父元素
我想從iframe的內容獲取父objet或父節點。
我的代碼:
a.html:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
</head>
<body>
<div>
<input type="button" value="click me" onClick="parent.getIframe(document);"/>
</div>
</body>
</html>
的index.php:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script>
var getIframe = function(obj){
// Iframe Element = $(obj).parent();
//or some things likes this.
}
</script>
</head>
<body>
<div>
<div id="myId">
<iframe src="a.html"></iframe>
</div>
</div>
</body>
</html>
現在我想,當我點擊 「點擊我」 按鈕。我想通過iframe的文檔對象獲取HTMLIFRAME對象元素。
我需要你的幫助。
如果我想要得到的div id包含IFRAME(「身份識別碼」),當我點擊「點擊我」在a.html頁面,我該怎麼辦? –
如果我想要得到div的ID包含iframe(「myId」),當我點擊「點擊我」在一個HTML頁面,我該怎麼做?現在我的解決方案是比較iframe的src屬性。但我不想使用它。因爲在我的頁面中有很多iframe。它可以是50或50以上的iframe =>我的程序將會很慢。我覺得這不是很好的理想。當點擊a.html的「點擊我」按鈕時。我們有頁面或contentDocument或contentWindow的文檔。爲什麼我們不能通過它們獲得Element IFrame。我需要你的幫助。 –
聽起來像你可能會遇到[同源政策](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy)。 – simonwo