Gue*_*ern 8 javascript iframe firefox focus onload
我将打开一个包含iframe的对话框,然后在打开对话框后将焦点设置在iframe中.
目前,我尝试在加载内容时关注iframe:
<iframe src="../credits.html" onload="this.contentWindow.focus()"></iframe>
Run Code Online (Sandbox Code Playgroud)
它适用于除Firefox以外的所有浏览器,我不明白为什么.
有人可以告诉我为什么吗?谢谢
Firefox 似乎没有 contentWindow 属性,你应该使用 contentDocument
更新:
经过一番小争论,我想出了一个更好的解决方案:
<html>
<head>
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$(function() {
$("#xframe").bind("load", function(){
$(this).contents().find("#xa").focus();
});
});
</script>
</head>
<body>
<iframe id="xframe" src="./y.html"></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在此示例中,我假设 y.html 中有一个输入字段具有“xa”id。它可以在 Chrome 和 Firefox 上运行。
旧答案
更好的是你应该使用jquery,比如:
<html>
<head>
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#xframe").focus();
});
</script>
</head>
<body>
<iframe id="xframe" src="./y.html"></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当然,您可以做一些额外的检查来查看 iframe 是否准备好。
更多信息可以在这里找到http://www.w3schools.com/jsref/prop_frame_contentwindow.asp
| 归档时间: |
|
| 查看次数: |
2880 次 |
| 最近记录: |