JS 用div 覆盖 html元素

例子,用层2覆盖层1  兼容IE6

 

<div id="div1"></div>

<div id="div2" style="position:absolute;"></div>

 

<script>

                        function getTop(e) {
                            var offset = e.offsetTop;
                            if (e.offsetParent != null) offset += getTop(e.offsetParent);
                            return offset;
                        }

                        function getLeft(e) {
                            var offset = e.offsetLeft;
                            if (e.offsetParent != null) offset += getLeft(e.offsetParent);
                            return offset;
                        }

                        var top = getTop(document.getElementById("div1"));
                        var left = getLeft(document.getElementById("div1"));
                        document.getElementById('div2').style.top = top + "px";
                        document.getElementById('div2').style.left = left + "px";
                        document.getElementById('div2').style.width = document.getElementById("div1").offsetWidth + "px";
                        document.getElementById('div2').style.height = document.getElementById("div1").offsetHeight + "px";

</script>

你可能感兴趣的:(html,function,IE,null,div)