日期:2014-05-17  浏览次数:20604 次

css+div 在指定位置创建层
<head>
<script language="javascript">
    function findPosX(obj){
        var curleft = 0;
        if (obj.offsetParent){
            while (obj.offsetParent){
                curleft += obj.offsetLeft
                obj = obj.offsetParent;
            }
        } else if (obj.x){
            curleft += obj.x;
        }
        return curleft;
    }
    
   function findPosY(obj){
        var curtop = 0;
        if (obj.offsetParent){
            while (obj.offsetParent){
                curtop += obj.offsetTop
                obj = obj.offsetParent;
            }
        } else if (obj.y){
            curtop += obj.y;
        }
        return curtop;
    }

    function autocomplete (){
        
        theObject = document.getElementById("autocompletediv");
        
        theObject.style.position = "absolute";
        theObject.style.visibility = "visible";
        theObject.style.border="1px red solid";
        theObject.style.width = "152px";
        
        var posx = 0;
        var posy = 0;
        
        posx = (findPosX (document.getElementById("yourname")) + 1);
        posy = (findPosY (document.getElementById("yourname")) + 23);
        
        theObject.style.left = posx + "px";
        theObject.style.top = posy + "px";
        var str = "12222222222<br>33333333<BR>444444444\n";
        theObject.innerHTML = str;
    }

</script>
</head>
<body>
    用户名:<input type="text" id="yourname" name="yourname">
    <input type="button" onclick="javascript:autocomplete();" value="click">
    <div id="autocompletediv" style="visibility:hidden;"></div>
</body>