iFrame getFocus 不会触发

iFrame getFocus won't trigger

我有一个 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 。所以我把上面的代码放在下面的代码中:

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