Jquery 获取 A 标签的closest() Iframe 返回未定义

发布于 2024-12-04 09:25:16 字数 1649 浏览 1 评论 0原文

我正在 onmouseover 调用我的 javascript 中的函数。在函数调用中,我包含 srcElement (它是一个 a 标签)。现在我需要获取该 a 标签最近的 Iframe 的 offsetWidth。但是当我尝试这个时:

alert($(srcElement).closest('iframe').offset().left);

我变得未定义!这也不起作用:

alert($(srcElement).closest('iframe').attr('title'));

但这可以工作并返回 iframe 内主体的 id:

alert($(srcElement).closest('body').attr('id'));

这是我在 html 中的函数调用:

showPeopleDetails('User_Matt', event.clientX, event.clientY, event.srcElement);

这是 javascript 函数:

function showPeopleDetailsNow(UserId, x, y, srcElement){
   currentwidth = $(document).width();
   currentheight = $(document).height();
   adjustwidth = $(srcElement).closest('iframe').offset().left;
   adjustheight = $(srcElement).closest('iframe').offset().top;
   var calculatedheight = currentheight - adjustheight;
   var calculatedwidth = currentwidth - adjustwidth;

现在我正在这样做,但这只能工作我的页面的一部分。由于 id 是由框架动态生成的。

   adjustwidth = parent.document.getElementById('ivuFrm_page0ivu0').contentWindow.document.getElementById('ivuFrm_page0ivu2').offsetWidth;
   adjustheight = parent.document.getElementById('ivuFrm_page0ivu0').contentWindow.document.getElementById('ivuFrm_page0ivu2').offsetHeight;

有没有什么方法可以获取 body 标签的偏移量(这是否是 iframe 的正确偏移量?我知道我可以获取 body 元素的宽度,但它不准确几个 px...显然,如果 Iframe 的正确偏移量更好! Iframe 与原始页面具有相同的端口、协议和主机。我只需要它与 Internet Explorer 8 一起使用。 非常感谢帮助!

更新:在 Felix Kling 和 scr4ve 提出解决方案后,我发现我无法使用 body 元素或 html 元素的偏移量!由于两个偏移量都返回 0,因此我假设 Iframe 元素是 dom 中唯一具有相对于文档的正确偏移量的对象。有什么建议如何获得特定的偏移量吗?

I'm calling a function in my javascript at onmouseover. In the function call I include the srcElement (which is an a-tag). Now I need to get the offsetWidth of the closest Iframe of that a-tag. But when i try this:

alert($(srcElement).closest('iframe').offset().left);

I get undefined! This doesn't work either:

alert($(srcElement).closest('iframe').attr('title'));

But this works and returns the id of the body inside the iframe:

alert($(srcElement).closest('body').attr('id'));

This is my function call in the html:

showPeopleDetails('User_Matt', event.clientX, event.clientY, event.srcElement);

This is the javascript function:

function showPeopleDetailsNow(UserId, x, y, srcElement){
   currentwidth = $(document).width();
   currentheight = $(document).height();
   adjustwidth = $(srcElement).closest('iframe').offset().left;
   adjustheight = $(srcElement).closest('iframe').offset().top;
   var calculatedheight = currentheight - adjustheight;
   var calculatedwidth = currentwidth - adjustwidth;

Right now I'm doing it this way, but this only works for one part of my page. As the id is generated dynamically by a framework.

   adjustwidth = parent.document.getElementById('ivuFrm_page0ivu0').contentWindow.document.getElementById('ivuFrm_page0ivu2').offsetWidth;
   adjustheight = parent.document.getElementById('ivuFrm_page0ivu0').contentWindow.document.getElementById('ivuFrm_page0ivu2').offsetHeight;

Is there any way to get the offset from the body tag (and would it be the correct offset of the iframe? I know that I can get the width of the body-element, but it is not accurate by a few px... Obviously even better would be the correct offset of the Iframe!
The Iframe has the same port, protocol and host as the original page. I only need it to work with internet explorer 8.
Help is much appreciated!

Update: After proposed solution from Felix Kling and scr4ve I found out that I can't use the offset of the body element or the html element! Since both offsets return 0, I assume that the Iframe element is the only object in the dom who has the correct offset relative to the document. Any suggestions how to get that specific offset?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2024-12-11 09:25:17

在对这个主题做了更多研究后,我找到了问题的解决方案,基于此 stackoverflow 问题中的成份_15939 提供的解决方案: 访问父级 Iframe 正如 Felix Kling 所建议的,文档层次结构中不存在 IFRAME,因此我在所有 Iframe 中搜索我的 a 的匹配主体 标签。我的最终代码的工作原理如下:

var searchbody = $(srcElement).closest('body');
var arrFrames = document.getElementsByTagName("iframe");

for(i = 0; i<arrFrames.length; i++){
    //I search and compare every IFRAME to the closest body of my srcElement
    try{
        if(arrFrames[i].id != 'PeopleDetailsIframe'){
            if($(arrFrames[i].contentWindow.document.body).is(searchbody)){
        //The found IFrame has to be the one containing my a-tag
                offsetwidth = $(arrFrames[i]).offset().left;
                offsetheight = $(arrFrames[i]).offset().top;
             }
             else{
                  //console.log("Iframe not found");
             }
         }
      }
      catch(e){
                //Iframe has security issues --> avoid it
      } 
}

希望这也可以帮助其他人!

After doing some more research on the subject I found the solution to my problem, by basing it on the solution provided by ingredient_15939 in this stackoverflow question: Access Parent Iframe As Felix Kling suggested the IFRAME does not exist in the document hierarchy, so I search all Iframes for the matching body of my a tag. My final code works as follows:

var searchbody = $(srcElement).closest('body');
var arrFrames = document.getElementsByTagName("iframe");

for(i = 0; i<arrFrames.length; i++){
    //I search and compare every IFRAME to the closest body of my srcElement
    try{
        if(arrFrames[i].id != 'PeopleDetailsIframe'){
            if($(arrFrames[i].contentWindow.document.body).is(searchbody)){
        //The found IFrame has to be the one containing my a-tag
                offsetwidth = $(arrFrames[i]).offset().left;
                offsetheight = $(arrFrames[i]).offset().top;
             }
             else{
                  //console.log("Iframe not found");
             }
         }
      }
      catch(e){
                //Iframe has security issues --> avoid it
      } 
}

Hope this can help someone else as well!

傻比既视感 2024-12-11 09:25:17

根据文档,.closest()仅考虑直接祖先。我猜你的 iframe 不是你的 a 标签的直接父级?仔细查看树遍历的文档。
.silbings() 可能符合您的需求。

顺便说一下,考虑使用console.log 而不是alert。

澄清后更新:在这种情况下,$("html").offset() 就足够了并且性能更高。框架中只有一个html标签,不需要.closest()。 OP 可能也对 .outerWidth() / 高度感兴趣。

According to the docs, .closest() only considers direct ancestors. I guess your iframe is not a direct parent of your a-tag? Have a closer look at the docs for Tree Traversal.
.silbings() may match your needs.

By the way, consider using console.log rather than alert.

Update after clarification: $("html").offset() is suffcient and more performant in this case. There's only one html tag in the frame, no need for .closest(). OP might be interested in .outerWidth() / Height, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文