js点击图片更换头像,input file



html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width = device-width,initial-scale=1,maximum-scale = 1.0,minimum-scale=1,user-scalable=0">
    <title>事件冒泡title>
   
    <style>
        /*个人中心文字部分*/
        .head{
            width: 100%;
            position:absolute ;
            z-index: 10;
            text-align: center;

        }
        .describe{
            display: inline-block;
            color: #ffffff;
            font-size: 18px;
            box-sizing: border-box;
            vertical-align: top;
            padding-top: 30px;
        }
        .portrait{
            margin-top: 25px;
            width: 100%;
            display: flex;
            justify-content: center;
            position: relative;
        }

        img{
            width: 72px;
            height: 72px;
        }

        .portrait-name{
            text-align: center;
            font-size: 27.5px;
            color: #ffffff;
        }
        #camera{
            font-size: 100px;
            left: 50%;
            top: 50%;
            opacity: 0;
            width: 128px;
            height: 30px;
            position: absolute;
            display:block;
        }
    style>
head>
<body>

<div class="head">
    <div class="describe">个人中心div>
    <div class="portrait">
        <div class="img-portrait" >
            <input type="file" id="camera" multiple="multiple"capture="camera" accept="image/*" >
            <img src="../3.jpg" alt="" id="imgUrl">
        div>
    div>
    <div class="portrait-name">张小李div>
div>
body>
<script src="../dist/vue.js">script>
<script src="../../jquery-1.9.1.min.js">script>
<script>
  
    $("#camera").change(function(e) {
        for (var i = 0; i < e.target.files.length; i++) {
            var file = e.target.files.item(i);
            var freader = new FileReader();
            freader.readAsDataURL(file);
            freader.onload = function(e) {
                var src = e.target.result;
                $("#imgUrl").attr("src",src);
            }
        }
    });
script>
html>

你可能感兴趣的:(方法)