js 实现在页面上传图片浏览

本文详细介绍了如何使用HTML和JS实现用户照片上传和预览功能,包括图片选择、预览和文件输入事件处理。

html代码

        <table style="border:none">
            <thead>
            <tr>
                <th class="userphoto">人员登记照</th>
                <th class="idcardphoto">身份证件照</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>
                    <p class="short-input ue-clear">
                        <?php if (!isset($datalist['userphoto'])): ?>
                            <img style="width:300px;height:250px;" src="/images/photo-empty.jpg" alt="" id="userphoto"
                                 name="userphoto"/>
                        <?php else: ?>
                            <img style="width:300px;height:250px;" src="<?php echo $datalist['userphoto']; ?>" alt=""
                                 id="userphoto" name="userphoto"/>
                        <?php endif; ?>
                    </p>
                </td>
                <td>
                    <p class="short-input ue-clear">
                        <?php if (!isset($datalist['idcardphoto'])): ?>
                            <img style="width:300px;height:250px;" src="/images/shenfenzheng.jpg" alt=""
                                 id="idcardphoto" name="idcardphoto"/>
                        <?php else: ?>
                            <img style="width:300px;height:250px;" src="<?php echo $datalist['idcardphoto']; ?>" alt=""
                                 id="idcardphoto" name="idcardphoto"/>
                        <?php endif; ?>
                    </p>
                </td>

           </tr>
            <tr>
                <td>
                    <p class="short-input ue-clear">
                        <input type="file" name="userphoto1" class="default" multiple=""
                               onchange="Preview(this,'userphoto','divuserphoto');" id="userphoto"/>
                    </p>
                </td>
                <td>
                    <p class="short-input ue-clear">
                        <input type="file" name="idcardphoto1" class="default" multiple=""
                               onchange="Preview(this,'idcardphoto','dividcardphoto');" id="idcardphoto"/>
                    </p>
                </td>

            </tr>
            </tbody>
        </table>

js代码

function Preview(obj, obj1, obj2) {
    if (document.all) {//document.all是页面内所有元素的一个集合,可以判断浏览器是否是IE
        $("#" + obj1).css("width", "0px");
        $("#" + obj1).css("height", "0px");
        $("#" + obj2).css("display", "block");
        alert("sd");
        document.getElementById('' + obj2 + '').filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = obj.value;
    } else {
        $('#' + obj1).show();
        var file = obj, objectURL = window.URL.createObjectURL(file.files[0]);
        $('#' + obj1).attr("src", objectURL);
    }
}

转载于:https://www.cnblogs.com/lqw4/p/4661261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值