图片上传和删除

简单的 相关链接:https://blog.51cto.com/niuben/3028616?abTest=51cto

<div id="bcd"></div>
<input type="file" id="abc">
  $(function() {
        $("#abc").change(function(e) {
            var imgBox = e.target;
            uploadImg($('#bcd'), imgBox)
        });

        function uploadImg(element, tag) {
            var file = tag.files[0];
            var imgSrc;
            if (!/image\/\w+/.test(file.type)) {
                alert("看清楚,这个需要图片!");
                return false;
            }
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function() {
                console.log(this.result);
                imgSrc = this.result;
                var imgs = document.createElement("img");
                $(imgs).attr("src", imgSrc);
                element.append(imgs);
            };
        }
    })

多张 相关链接:https://blog.csdn.net/qq_43404873/article/details/111405627

<!DOCTYPE html>
<html lang="en">

<link>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>标签</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-ui/2.2.10/semantic.min.css">
<link rel="stylesheet" href="../static/css/me.css"></link>
</head>
<style>
    .center {
        text-align: center;
        margin-top: 30px;
    }
</style>

<body>
    <!-- 中间内容 -->
    <div class="m-container-small m-padded-tb-big">
        <div class="ui container">
            <form class="ui form">
                <div class="field">
                    <label>标题</label>
                    <input type="text" name="text" placeholder="标题">
                </div>
                <div class="field">
                    <input type="file" multiple id="upload_file" name="files" style="display: none;">
                    <div class="imageContainer">
                        <div class="ui centered image" style="width:150px; height: 150px; background-color: white; border: 2px dashed black; display: inline-block;">
                            <div class="center">
                                <img src="../static/images/add.png" alt="" class="ui  mini centered image">
                                <p style="margin-top: 8px;" id="uploadMoreImages">上传图片</p>
                            </div>
                        </div>

                    </div>

                </div>

                <button class="ui button" type="submit">上传</button>
        </div>
        </form>
    </div>
    </div>
</body>

</html>
 	.m-padded-tb-big {
        padding-top: 3em !important;
        padding-bottom: 3em !important;
    }
    
    .m-container-small {
        max-width: 60em !important;
        margin: auto !important;
    }

    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/semantic-ui/2.2.10/semantic.min.js"></script>
    <script>
        $('.menu_toggle').click(function() {
            $(".m-item").toggleClass('m-mobile-hide')
        })
        $(".center").click(function() {
            $("#upload_file").click();
        })
        var img
        $("#upload_file").change(function() {
            var images = []; //定义一个空的数组用来储存img路径
            var files = $(this).get(0).files;
            console.log(files)
            var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/; //只能上传图片
            $($(this)[0].files).each(function() {
                var file = $(this);
                console.log(file)
                if (regex.test(file[0].name.toLowerCase())) {
                    //如果是图片
                    var reader = new FileReader();
                    reader.onload = function(e) { //读取图片成功会执行这个函数
                        var div = $("<div></div>")
                        div.attr("style", "position: relative; display:inline-block;")
                        var delImg = $("<img/>")
                        delImg.attr("style", "position: absolute; right: 15px; top: 15px; width: 20px;height: 20px; z-index:1; display:none;")
                        delImg.attr("src", "../static/images/delete3.png")
                        delImg.attr("class", "delImg")

                        var img = $("<img />");
                        img.attr("style", "display: inline-block; height:150px; margin:8px;");
                        img.attr("src", e.target.result);
                        img.attr("class", "ui small centered image")
                        div.append(delImg)
                        div.append(img);
                        $(".imageContainer").prepend(div);
                    }
                    reader.readAsDataURL(file[0]);
                    $("#uploadMoreImages").text("上传更多图片")
                } else {
                    alert(file[0].name + "不是一张图片");
                    return false;
                }
            })
        })

        //鼠标悬浮显示删除按钮
        $(".imageContainer").on("mouseover mouseout", "img", function(event) {
            // console.log($(this).prev()[0])
            if (event.type == "mouseover") {
                //鼠标悬浮
                $($(this).prev()[0]).show();
            } else if (event.type == "mouseout") {
                //鼠标离开
                $($(this).prev()[0]).hide();
            }
        })

        //删除图片
        $(".imageContainer").on("click", ".delImg", function() {
            $($(this).next()[0]).remove();
        })
    </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值