关闭

JS 用div 覆盖 html元素

标签: divhtmlfunctionnullie
3240人阅读 评论(0) 收藏 举报
分类:

例子,用层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>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:620207次
    • 积分:6795
    • 等级:
    • 排名:第3544名
    • 原创:103篇
    • 转载:123篇
    • 译文:0篇
    • 评论:49条
    最新评论