[英]How can I get the position of an iframe using js in the iframe?
該代碼可能看起來像這樣:
<!--index.htm-->
<iframe src="abc.htm"></iframe>
我可以控制abc.htm,現在我想知道iframe在頁面index.htm中的位置,該怎么辦? 我無法在index.htm中進行編碼。
我使用window.parent
,但由於跨域問題我無法獲得頂部窗口,那么我該怎么做才能達到目標?
非常感謝!
這就是使用JavaScript獲取頂部和左側元素的位置的方法。
只需將Id添加到您的iframe
如果您不願意,則可以使用document.querySelector()
進行選擇。
<iframe src="abc.htm" id = "el"></iframe>
var el = document.getElementById("el");
el.innerHTML = el.offsetTop +" " +el.offsetLeft;
console.log("Top :" + el.offsetTop +" " +"Left :" +el.offsetLeft);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.