返回列表

解决select无法自动定位到选中项的问题

默认分类 2011-08-25 02:08:07

Untitled 3

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Focus Select - haiyang.me</title>
<script type="text/javascript">
<!--
var sel,opt;
function createSelect(){
var i,j,k;
sel = document.getElementById('select').appendChild(document.createElement("select"));
sel.multiple = false;
sel.size = 10;
for(i=1;i<20;i++){
opt = sel.appendChild(document.createElement("option"));
opt.appendChild(document.createTextNode(i));
}
}
function focusSelect(){
sel.options(15).selected = true;
}
//-->
</script>
</head>

<body>
<button onclick="createSelect()">create</button>
<button onclick="focusSelect()">focus</button>
<div id="select"></div>
</body>

</html>