【问题标题】:iFrame getFocus won't triggeriFrame getFocus 不会触发
【发布时间】:2017-03-23 05:28:40
【问题描述】:

我有一个 iframe 如下:

<iframe name="richTextField" id="richTextField" style="border:#000 1px solid; width:700px; height:300px;"></iframe>

我有一些 javascript 应该在 iframe 获得或失去焦点时触发,但在 Firefox 上它不起作用,除非我在正手插入警报。脚本如下:

<script type="text/JavaScript">
    document.getElementById('richTextField').contentWindow.document.designMode="on";
    document.getElementById('richTextField').contentWindow.document.close();

    if(navigator.userAgent.indexOf("Firefox") != -1){
        //When alert below is removed, the blur/focus doesn't work in FireFox
        alert("what?");
        $("#richTextField").contents().bind('blur', function(){
//            blur functions
            console.log("Firefox Blur");
        });
        $("#richTextField").contents().bind('focus', function(){
//                focus functions
            console.log("Firefox Focus");
        });
    } else {
        $("#richTextField").contents().find("body").blur(function(){
//                blur functions
            console.log("Blur");
        });
        $("#richTextField").contents().find("body").focus(function(){
//                focus functions
            console.log("Focus");
        });
    }
</script>

【问题讨论】:

    标签: javascript jquery firefox iframe focus


    【解决方案1】:

    我犯了一个简单的错误,即在文档准备好之前执行脚本。所以我不得不稍微改变一下javascript。所以我把上面的代码放在下面的代码中:

    $(document).ready(function() {
        //Above code without the script tags and without the alert
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多