CSS样式:
<style type ="text/css">
#divShow{position:absolute; z-index:9999; overflow:auto; max-height:200px; display:none;border:1px solid #ddd}
#divShow ul{padding:0px;margin:0px;list-style:none;}
#divShow ul li{cursor:pointer;padding:0px 5px; line-height:25px;height:25px;}
#divShow ul li:hover{background:#F6F6F6}
</style>
HTML代码:
<input type='text' id='txtInput' />
<div id='divShow'><ul><li>数据正在加载中...</li></ul></div>
<script type="text/javascript">
var vid=document.getElementById("j_member_id");
var txtInput ;
var divShow ;
</script>
<script language="javascript" src="/js/xlk.js"></script>
xlk.js文件代码:
window.onload=function()
{
txtInput =document.getElementById("txtInput");
divShow = document.getElementById("divShow");
var p =getAbsPoint(txtInput);
divShow.style.left = p.x +'px';
divShow.style.top = p.y + txtInput.offsetHeight + 'px';
txtInput.onclick = divShow.onclick=function(e)
{
divShow.style.display = 'block';
e = e||event;
var t = e.target||e.srcElement
if(t.tagName.toLowerCase()=='li')
{
if(t.id==''){
txtInput.value=''; vid.value='';
}else{
txtInput.value = t.innerHTML;
vid.value=((t.id).split('-'))[1];
}
divShow.style.display = "none";
return;
}
if(e && e.stopPropagation){
//W3C取消冒泡事件
e.stopPropagation();
}else{
//IE取消冒泡事件
window.event.cancelBubble = true;
}
};
document.body.onclick=function(e){divShow.style.display = "none";};
};
function getAbsPoint(e)// 获取绝对坐标
{
var x = e.offsetLeft;
var y = e.offsetTop;
while(e = e.offsetParent)
{
x += e.offsetLeft;
y += e.offsetTop;
}
return {"x": x, "y": y};
}