在一个页面上,我有一个包含输入框的 Iframe,如果我在 FireFox 中选择其中一个输入框并使用 document.activeElement
,我会得到 IFrame。没关系,我只是使用那个 IFrame 并获取它的 contentDocument,保存它,然后再次执行 activeElement,现在我得到了输入框。
但是,在 Chrome 和 Safari 中,当我选择 IFrame 中的一个框并执行 document.activeElement
时,我得到了 body 元素。如果我选择 IFrame 之外的元素,document.activeElement
将完美运行。
如何获取案例中的事件元素?
最佳答案
不确定这是否适合您,但您可以尝试类似的方法。此外,我相信您将通过以下方法受到相同域的限制。
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);
}
关于javascript - document.activeElement 在 Chrome 和 Safari 中返回 body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3586933/