日期:2014-05-16 浏览次数:20445 次
<input id="q" type=text size="50" style="margin-left:200px" onkeyup="search()">
<script>
function search()
{
//计算文本框的位置
var q = document.getElementById("q");
var left2 = q.offsetLeft; //相对亲的左边
var top2 = q.offsetTop;
var p = q.offsetParent; //父
while (p)
{
left2 = left2 + p.offsetLeft;
top2 = top2 + p.offsetTop;
p = p.offsetParent;
}
//查找此层,如果文本框没有内容时,就删除此层
var div = document.getElementById("mydiv");
if (q.value=="")
{
if (div) //层存在就删除
{
document.body.removeChild(mydiv);
}
}
//如果层不存在,就创建
if (!div)
{
div = document.createElement("div");
div.id = "mydiv";
with (div.style)
{
left = left2;
top = top2 + q.offsetHeight;
width = q.offsetWidth;
height = 500;
position = "absolute";
border = "1px solid green";
}
document.body.appendChild(div);
}
}
</script>