我使用
<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
,会成功的