【问题标题】:Set focus on Iframe Input field onload将焦点设置在 Iframe 输入字段 onload
【发布时间】:2016-03-02 21:12:28
【问题描述】:

我正在尝试将焦点设置在 iframe onload 内的输入字段上。 iframe 没有 id 或类,所以我使用标签来获取元素,但是当我这样做时,我得到一个错误 "TypeError: Cannot read property 'getElementById' of undefined at window.onload"

window.onload = function() {
document.getElementsByTagName('iframe').contentWindow.document.getElementById("form_002b_fld_0_fn").focus();
};

附言 将脚本放在标题上的唯一选项,我不允许编辑输入框以添加自动对焦,因为它是一个没有 html 代码的表单生成器

【问题讨论】:

    标签: javascript jquery html iframe


    【解决方案1】:

    在这种情况下,contentWindow 未定义,因为您无权访问包含其他域内容的 iFrame。

    要使其正常工作的唯一方法是请您正在使用的表单构建应用程序的所有者为您执行此操作。

    【讨论】:

    • 我实际上使用的是 iframe 的同一个域,例如domain.com 和 domain.com/iframe.html
    • 其实contentWindow是定义的,contentWindow.document是未定义的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    相关资源
    最近更新 更多