【问题标题】:document.activeElement returns body in Chrome and Safaridocument.activeElement 在 Chrome 和 Safari 中返回正文
【发布时间】:2010-08-28 06:06:12
【问题描述】:

在一个页面上,我有一个包含输入框的 Iframe,如果我在 FireFox 中选择其中一个框并使用 document.activeElement,我会得到 IFrame。没关系,我只是使用那个 IFrame 并获取它的 contentDocument,保存它,然后再次执行 activeElement,现在我得到了输入框。

但是,在 Chrome 和 Safari 中,当我选择 IFrame 中的一个框并执行 document.activeElement 时,我得到了 body 元素。如果我选择 IFrame 之外的元素,document.activeElement 可以完美运行。

在我的情况下如何获取活动元素?

【问题讨论】:

    标签: javascript html iframe


    【解决方案1】:

    不确定这是否完全适合您,但您可以尝试类似的方法。另外,我相信您会通过以下方法受到相同的域限制。

    function showme() {
    
        var currentDoc = document;
    
        if (document.activeElement == document.body) {
            currentDoc = window.frames['child-iframe'].document;
        }
    
        if (currentDoc.activeElement.type == "text" 
            || currentDoc.activeElement.type == "textarea" 
            || currentDoc.activeElement.type == "checkbox") {
            currentDoc.activeElement.style.color = "red";
        }
    
    }
    
    
    window.onload = function() {
        setTimeout("showme()", 5000);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 2015-02-27
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 2023-04-06
      相关资源
      最近更新 更多