2016-11-09 5 views
0

У меня есть IFRAME следующим образом:GetFocus не плавающий фрейм будет вызывать

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

И у меня есть некоторые JavaScript, который должен вызвать, когда IFrame получает или теряет фокус, но на светлячок это не если я не вставил предупреждение на передний план. Сценарий выглядит следующим образом:

<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> 

ответ

0

Я сделал простую ошибку при выполнении сценария до того, как документ был готов. Поэтому мне пришлось немного изменить javascript. Поэтому я поставил выше код в следующем коде:

$(document).ready(function() { 
    //Above code without the script tags and without the alert 
}); 
Смежные вопросы