【问题标题】:createElement in parent vs. iframe父级与 iframe 中的 createElement
【发布时间】:2012-11-26 03:58:42
【问题描述】:

我有一个模块化网页,每个模块都是一个可以与父页面交互的 iframe。特别是 iframe 向父级添加新元素(导航、选项卡等)。所有页面都在同一个域中。

到目前为止,我一直在 iframe 中使用这种模式:

var newDiv=document.createElement("div");
// do stuff with newDiv
parent.document.body.appendChild(newDiv);

我最近突然想到,因为 newDiv 将附加到父级,所以这样做会更有意义:

var newDiv=parent.document.createElement("div");
// do stuff with newDiv
parent.document.body.appendChild(newDiv);

我的问题:新元素是用 document.createElement 还是 parent.document.createElement 创建的有什么区别吗?

【问题讨论】:

    标签: javascript iframe appendchild createelement


    【解决方案1】:

    这行得通吗?如果是这样,那么我认为(不是 100% 肯定)两者都是相同的,因为 createElementDOM 方法和 document 对象的成员,并且您可以同时访问两个文档(两个对象)并且两者包含相同的成员函数(createElement 在两者中都可用),在这种情况下,一个在 iframe 中,另一个在它的父级中。因此,看起来您正在从一个来源而不是另一个来源调用方法,IMO

    就像我说的,这只是一个意见,所以请等待专家的更多答案。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多