Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将上载的图像永久替换为占位符图像_Javascript_Jquery_Html_Image_Jquery File Upload - Fatal编程技术网

Javascript 将上载的图像永久替换为占位符图像

Javascript 将上载的图像永久替换为占位符图像,javascript,jquery,html,image,jquery-file-upload,Javascript,Jquery,Html,Image,Jquery File Upload,我一直在尝试用我上传的占位符图像替换图像的功能,这样我的客户就不需要登录(任何CMS)的后端,而且他们几乎都是非技术人员 下面的代码将显示占位符图像以及“上载文件按钮”。一旦他们上传他们的图像,我会删除选择文件选项。是否有可能将他们上传的图像存储在网站的文件夹中 HTML: 请在此处查看尝试使用此 <input id="src" type="file"/> <input type="button" id="rem" value="remove" style="visibilit

我一直在尝试用我上传的占位符图像替换图像的功能,这样我的客户就不需要登录(任何CMS)的后端,而且他们几乎都是非技术人员

下面的代码将显示占位符图像以及“上载文件按钮”。一旦他们上传他们的图像,我会删除选择文件选项。是否有可能将他们上传的图像存储在网站的文件夹中

HTML:

请在此处查看

尝试使用此

<input id="src" type="file"/>
<input type="button" id="rem" value="remove" style="visibility:hidden;" onclick="myfun()">
<img id="target" src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/No_pub.svg/150px-No_pub.svg.png" style="width:100px;height:100px;"/> 

<script>
function showImage(src,target) {
var fr=new FileReader();
fr.onload = function(e) { target.src = this.result; };
src.addEventListener("change",function() {
fr.readAsDataURL(src.files[0]);
document.getElementById("src").style.visibility = "hidden";
document.getElementById("rem").style.visibility = "visible";

});

}
var src = document.getElementById("src");
var target = document.getElementById("target");
showImage(src,target);
function myfun(){
target.src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/No_pub.svg/150px-No_pub.svg.png";
document.getElementById("src").style.visibility = "visible";
document.getElementById("rem").style.visibility = "hidden";
};
</script>

函数showImage(src,目标){
var fr=new FileReader();
fr.onload=function(e){target.src=this.result;};
src.addEventListener(“更改”,函数(){
fr.readAsDataURL(src.files[0]);
document.getElementById(“src”).style.visibility=“hidden”;
document.getElementById(“rem”).style.visibility=“可见”;
});
}
var src=document.getElementById(“src”);
var target=document.getElementById(“目标”);
showImage(src,target);
函数myfun(){
target.src=”https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/No_pub.svg/150px-No_pub.svg.png";
document.getElementById(“src”).style.visibility=“可见”;
document.getElementById(“rem”).style.visibility=“隐藏”;
};

当然,您需要一个后端。
$('#blah').show();
$('#remove').hide();  
function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#imgInp").change(function(){
        if( $('#imgInp').val()!=""){

            $('#remove').show();
            $('#blah').show('slow');
      }
        else    {
                     $('#remove').hide();
        $('#blah').hide('slow');
        }
        readURL(this);
    });


    $('#remove').click(function(){
          $('#imgInp').val('');
          $(this).hide();
          $('#blah').hide('slow');
 $('#blah').attr('src','http://upload.wikimedia.org/wikipedia/commons/thumb/4/40/No_pub.svg/150px-No_pub.svg.png');
});
<input id="src" type="file"/>
<input type="button" id="rem" value="remove" style="visibility:hidden;" onclick="myfun()">
<img id="target" src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/No_pub.svg/150px-No_pub.svg.png" style="width:100px;height:100px;"/> 

<script>
function showImage(src,target) {
var fr=new FileReader();
fr.onload = function(e) { target.src = this.result; };
src.addEventListener("change",function() {
fr.readAsDataURL(src.files[0]);
document.getElementById("src").style.visibility = "hidden";
document.getElementById("rem").style.visibility = "visible";

});

}
var src = document.getElementById("src");
var target = document.getElementById("target");
showImage(src,target);
function myfun(){
target.src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/No_pub.svg/150px-No_pub.svg.png";
document.getElementById("src").style.visibility = "visible";
document.getElementById("rem").style.visibility = "hidden";
};
</script>