body onload在IE浏览器中不工作

body onload not working in IE browser

本文关键字:工作 浏览器 IE onload body      更新时间:2023-09-26

我使用

<body marginheight='0' topmargin='0' leftmargin='0' onLoad='setFocus()'>

在javascript中我使用

<script language='JavaScript'>
function setFocus(){
  alert("Hello");
document.frm.txtName.focus()
} 
</script>

这在IE中不工作,但在FF和chrome浏览器中工作完美

有什么想法吗?

尝试将document.frm.txtName.focus()更改为document.forms[0].txtName.focus()。这将在IE中工作。

确保你的<form name="frm">是页面上的第一个表单。即:在此表单之前,文档中不应该有任何其他<form>元素。

如果你不能这样做,只需添加id属性到你的txtName文本字段。例如:

<input type="text" name="txtName" id="someId">

在你的函数setFocus中写:

function setFocus(){
   document.getElementById('someId').focus()
}

试试window.onload,会成功的

相关文章: