js动态创建图片

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">

        //1window.onload事件
        window.onload = function () {
            document.getElementById('btn').onclick = function () {
                //初始化数据.
                var datas = {
                    "Images/1.jpg": ["Images/1_1.jpg", "张三", "178cm"],
                    "Images/2.jpg": ["Images/2_1.jpg", "李四", "170cm"]
                };
                //循环遍历每一条数据.for in
                var dvobj = document.getElementById('dvSmallPic');
                for (var key in datas) {
                    //1.创建一个img图片框.
                    var imgSmallObj = document.createElement('img');
                    imgSmallObj.src = key;
                    imgSmallObj.alt = datas[key][1];
                    imgSmallObj.style.marginRight = '32px'; //设置动态创建的图片之间的距离.marginRight.

                    //2给每个小图片添加onmouseover事件.
                   
                    imgSmallObj.onmouseover = function () {
                        //获取层
                        var dvobjs = document.getElementById('dvDetails');
                        dvobjs.style.display = 'block';
                        //设置层的位置
                        dvobjs.style.position = 'absolute';
                        dvobjs.style.left = this.offsetLeft + 'px';
                        dvobjs.style.top = this.offsetTop + this.offsetHeight + 'px';

                    };
                    dvobj.appendChild(imgSmallObj);

                };
                

            };
        };
    </script>
</head>
<body>
    <input type="button" name="name" value="动态创建图片"id="btn" />
    <div id="dvSmallPic">   
    </div>
    <div id="dvDetails">
        <img id="imgBigPic" style="display:none; border:1px solid blue; width:300px;height:240px;" src="#" alt="Alternate Text" />
        <p id="pname"></p>
        <p id="pheight"></p>
    </div>
</body>
</html>

转载于:https://www.cnblogs.com/nqsan/p/3194735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值