[英]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.