set focus to iframe body/content in firefox?

☆樱花仙子☆ 提交于 2019-12-23 07:46:16

问题


i have an ifram, which has hidden visibility. i click on button - iframe will be visible and i want to focus on its body/content. because now i must click on button and then into iframe, so thats two clicks. i want to do it on one click, but dont know how to focus in firefox.

in explorer it is ok:

var iframe_window = window.frames["myFrame"];
iframe_window.document.body.focus();

try lot of examples for firefox, but they dont work. anybody know how to do it in firefox ? thanks.


回答1:


Try to set the focus to iframe_window first and to your actual content next:

iframe_window.focus();
iframe_window.contentDocument.body.focus();



回答2:


Does

iframe_window.contentDocument.body.focus();

work?




回答3:


I think you might have to actually focus something inside the iFrame; the iFrame itself wouldn't do anything with the focus, but maybe you can put focus a DIV inside it, or even a widget?




回答4:


Try setting the focus to the <iframe> element itself.




回答5:


Not every element supports .focus(). Can you focus the element, you are trying to focus programmatically, using keyboard/mouse?

Focus can go to elements form elements or 'a' tags or a span with contant-editable property set to true.

Find such an element in the iframe and try focusing on that. Should work !!




回答6:


I had the same problem (only in firefox), I seem to have solved it by setting the focus on the iframe only after you're certain that the iframe has completely loaded. In my iframe html:

<body onLoad="init();>

Javascript inside the iframe page:

this.init = function(){
    parent.focusframe();
}

And this javascript is in the parent window:

this.focusframe = function(){
    var fr = document.getElementById("gameframe");
    fr.contentWindow.focus();
}


来源:https://stackoverflow.com/questions/1508348/set-focus-to-iframe-body-content-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!