js 动态生成div显示id

<!DOCTYPE html>


<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        onload = function () {
            var body = document.getElementsByTagName('body')[0];
            var ps = document.getElementsByTagName('p');
            var dv = document.createElement('div');
            dv.style.width = '100px';
            dv.style.height = '100px';
            body.appendChild(dv);
            dv.style.display = 'none';
            for (var i = 0; i < ps.length; i++) {
                ps[i].onmouseover = function (e) {
                    dv.style.position = 'absolute';                    
                    dv.style.left = e.clientX + 'px';
                    dv.style.top = e.clientY + 'px';
                    dv.innerHTML = this.id;
                    dv.style.display = 'block';



                };
                ps[i].onmouseout = function () {
                    dv.style.display = 'none';
                }

            }
        }
    </script>
</head>
<body>
   <p id="1">1</p>
   <p id="2">2</p>
   <p id="3">3</p>
   <p id="4">4</p>
   <p id="5">5</p>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/80263994
今日推荐