问题描述
在页面上我有一个包含输入框的Iframe,如果我在FireFox中选择其中一个框并使用 document.activeElement
,我会得到IFrame。多数民众赞成好吧,我只是使用那个IFrame并获取其contentDocument,保存它,然后再次执行activeElement,现在我得到输入框。
On a page I have an Iframe that contains input boxes, and if I select one of those boxes in FireFox and use document.activeElement
, I get the IFrame. Thats okay, I just use that IFrame and get its contentDocument, save it, then do activeElement again and now I get the input box.
但是,在Chrome和Safari中,当我选择IFrame中的一个框并执行 document.activeElement
时,我得到了body元素。如果我在IFrame之外选择一个元素, document.activeElement
可以正常工作。
However, in Chrome and Safari, when I select one of the boxes inside the IFrame and do document.activeElement
, I get the body element. If I select an element outside the IFrame, document.activeElement
works perfectly.
如何获取活动元素在我的情况下?
How can I get the active element in my case?
推荐答案
不确定这是否适合您,但您可以尝试类似的方法。此外,我相信您将受到以下方法的相同域名限制。
Not sure if this will work for you exactly but you can try a similar approach to this. Also, I believe you'll be bound to same domain restrictions with the approach below.
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);
}
这篇关于document.activeElement在Chrome和Safari中返回body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!