javascript - 单击清除文件上传

标签 javascript jquery html css

我有以下 jsFiddle Demo 允许用户上传图片并显示。

但是,当用户想要点击'x'来删除图像时,它不起作用?

谁能解释一下我是如何做到这一点的?

$("input[type='file']").on("change", function(event1) {
  src1 = URL.createObjectURL(event1.target.files[0]);
  this.parentNode.parentNode.querySelector("label").style.backgroundImage = 'url(' + src1 + ')';
  this.parentNode.parentNode.querySelector("label span").style.display = 'none';
  this.parentNode.parentNode.querySelector("button").style.display = 'block';
});
.form-file-upload-container {
  float: left;
  width: 100%;
  margin-bottom: 12px;
  margin-top: 12px;
}
@media (min-width: 640px) {
  .form-file-upload-container {
    margin-bottom: 20px;
    margin-top: 20px
  }
}
.form-file-upload {
  position: relative;
  float: left;
  width: 48.5%;
  height: auto;
  margin-right: 3%;
  margin-bottom: 3%;
}
@media (min-width: 450px) {
  .form-file-upload {
    width: 31.3333%
  }
}
@media (min-width: 750px) {
  .form-file-upload {
    width: 22.75%
  }
}
@media (min-width: 1600px) {
  .form-file-upload {
    width: 14.1666667%;
    margin-right: 2%
  }
}
.form-file-upload:nth-child(2n) {
  margin-right: 0
}
@media (min-width: 450px) {
  .form-file-upload:nth-child(2n) {
    margin-right: 3%
  }
}
@media (min-width: 1600px) {
  .form-file-upload:nth-child(2n) {
    margin-right: 2%
  }
}
@media (min-width: 450px) {
  .form-file-upload:nth-child(3n) {
    margin-right: 0
  }
}
@media (min-width: 750px) {
  .form-file-upload:nth-child(3n) {
    margin-right: 3%
  }
}
@media (min-width: 1600px) {
  .form-file-upload:nth-child(3n) {
    margin-right: 2%
  }
}
@media (min-width: 750px) {
  .form-file-upload:nth-child(4n) {
    margin-right: 0
  }
}
@media (min-width: 1600px) {
  .form-file-upload:nth-child(4n) {
    margin-right: 2%
  }
}
@media (min-width: 1600px) {
  .form-file-upload:nth-child(6n) {
    margin-right: 0
  }
}
.form-file-upload-inner {
  width: 100%;
  height: 0;
  padding-top: 133.33333%
}
.form-file-upload label {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: white;
  z-index: 2;
  cursor: pointer;
  border: 1px solid #AFAFB4;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  color: #AFAFB4;
  -webkit-transition: background 0.2s ease, border 0.2s ease;
  -moz-transition: background 0.2s ease, border 0.2s ease;
  -o-transition: background 0.2s ease, border 0.2s ease;
  transition: background 0.2s ease, border 0.2s ease
}
.form-file-upload span {
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  font-size: 15px;
  text-align: center;
  -webkit-transform: translateY(-50%);
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  -o-transform: translateY(-50%);
  transform: translateY(-50%)
}
.form-file-upload span:before {
  content: '';
  background: url(../img/image-upload-icon.png) no-repeat center center #fff;
  background: url(../img/image-upload-icon.svg) no-repeat center center #fff;
  background-size: 65px 56px;
  display: block;
  width: 65px;
  height: 56px;
  margin: 0 auto;
  margin-bottom: 5px
}
.form-file-upload input {
  position: relative;
  opacity: 0;
  z-index: 1
}
.form-file-upload .close {
  display: none;
  position: absolute;
  top: -13px;
  right: -13px;
  width: 26px;
  height: 26px;
  line-height: 18px;
  font-size: 18px;
  text-align: center;
  line-height: 26px;
  background: white;
  z-index: 3;
  -webkit-transition: background 0.2s ease, color 0.2s ease;
  -moz-transition: background 0.2s ease, color 0.2s ease;
  -o-transition: background 0.2s ease, color 0.2s ease;
  transition: background 0.2s ease, color 0.2s ease
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-file-upload">
  <div class="form-file-upload-inner">
    <label for="FileOne"><span>Select Image</span>
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileOne" size="40" class="wpcf7-form-control wpcf7-file" id="FileOne" aria-invalid="false" /></span>
    <button class="close" type="button">x</button>
  </div>
</div>
<div class="form-file-upload">
  <div class="form-file-upload-inner">
    <label for="FileTwo"><span>Select Image</span>
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileTwo" size="40" class="wpcf7-form-control wpcf7-file" id="FileTwo" aria-invalid="false" /></span>
    <button class="close" type="button">x</button>
  </div>
</div>
<div class="form-file-upload">
  <div class="form-file-upload-inner">
    <label for="FileThree"><span>Select Image</span>
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileThree" size="40" class="wpcf7-form-control wpcf7-file" id="FileThree" aria-invalid="false" /></span>
    <button class="close" type="button">x</button>
  </div>
</div>

最佳答案

您需要为您的按钮添加点击事件。

这是一个fiddle这是一个实时工作代码:

$("input[type='file']").on("change", function(event1) {
  src1 = URL.createObjectURL(event1.target.files[0]);
  this.parentNode.parentNode.querySelector("label").style.backgroundImage = 'url(' + src1 + ')';
  this.parentNode.parentNode.querySelector("label span").style.display = 'none';
  this.parentNode.parentNode.querySelector("button").style.display = 'block';
});
$("button").click(function (){
  this.parentNode.parentNode.querySelector("label").style.backgroundImage = 'none';
  this.parentNode.parentNode.querySelector("label span").style.display = 'block';
  this.parentNode.parentNode.querySelector("button").style.display = 'none';
});
.form-file-upload-container {
  float: left;
  width: 100%;
  margin-bottom: 12px;
  margin-top: 12px;
}
@media (min-width: 640px) {
  .form-file-upload-container {
    margin-bottom: 20px;
    margin-top: 20px
  }
}
.form-file-upload {
  position: relative;
  float: left;
  width: 48.5%;
  height: auto;
  margin-right: 3%;
  margin-bottom: 3%;
}
@media (min-width: 450px) {
  .form-file-upload {
    width: 31.3333%
  }
}
@media (min-width: 750px) {
  .form-file-upload {
    width: 22.75%
  }
}
@media (min-width: 1600px) {
  .form-file-upload {
    width: 14.1666667%;
    margin-right: 2%
  }
}
.form-file-upload:nth-child(2n) {
  margin-right: 0
}
@media (min-width: 450px) {
  .form-file-upload:nth-child(2n) {
    margin-right: 3%
  }
}
@media (min-width: 1600px) {
  .form-file-upload:nth-child(2n) {
    margin-right: 2%
  }
}
@media (min-width: 450px) {
  .form-file-upload:nth-child(3n) {
    margin-right: 0
  }
}
@media (min-width: 750px) {
  .form-file-upload:nth-child(3n) {
    margin-right: 3%
  }
}
@media (min-width: 1600px) {
  .form-file-upload:nth-child(3n) {
    margin-right: 2%
  }
}
@media (min-width: 750px) {
  .form-file-upload:nth-child(4n) {
    margin-right: 0
  }
}
@media (min-width: 1600px) {
  .form-file-upload:nth-child(4n) {
    margin-right: 2%
  }
}
@media (min-width: 1600px) {
  .form-file-upload:nth-child(6n) {
    margin-right: 0
  }
}
.form-file-upload-inner {
  width: 100%;
  height: 0;
  padding-top: 133.33333%
}
.form-file-upload label {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: white;
  z-index: 2;
  cursor: pointer;
  border: 1px solid #AFAFB4;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  color: #AFAFB4;
  -webkit-transition: background 0.2s ease, border 0.2s ease;
  -moz-transition: background 0.2s ease, border 0.2s ease;
  -o-transition: background 0.2s ease, border 0.2s ease;
  transition: background 0.2s ease, border 0.2s ease
}
.form-file-upload span {
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  font-size: 15px;
  text-align: center;
  -webkit-transform: translateY(-50%);
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  -o-transform: translateY(-50%);
  transform: translateY(-50%)
}
.form-file-upload span:before {
  content: '';
  background: url(../img/image-upload-icon.png) no-repeat center center #fff;
  background: url(../img/image-upload-icon.svg) no-repeat center center #fff;
  background-size: 65px 56px;
  display: block;
  width: 65px;
  height: 56px;
  margin: 0 auto;
  margin-bottom: 5px
}
.form-file-upload input {
  position: relative;
  opacity: 0;
  z-index: 1
}
.form-file-upload .close {
  display: none;
  position: absolute;
  top: -13px;
  right: -13px;
  width: 26px;
  height: 26px;
  line-height: 18px;
  font-size: 18px;
  text-align: center;
  line-height: 26px;
  background: white;
  z-index: 3;
  -webkit-transition: background 0.2s ease, color 0.2s ease;
  -moz-transition: background 0.2s ease, color 0.2s ease;
  -o-transition: background 0.2s ease, color 0.2s ease;
  transition: background 0.2s ease, color 0.2s ease
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-file-upload">
  <div class="form-file-upload-inner">
    <label for="FileOne"><span>Select Image</span>
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileOne" size="40" class="wpcf7-form-control wpcf7-file" id="FileOne" aria-invalid="false" /></span>
    <button class="close" type="button">x</button>
  </div>
</div>
<div class="form-file-upload">
  <div class="form-file-upload-inner">
    <label for="FileTwo"><span>Select Image</span>
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileTwo" size="40" class="wpcf7-form-control wpcf7-file" id="FileTwo" aria-invalid="false" /></span>
    <button class="close" type="button">x</button>
  </div>
</div>
<div class="form-file-upload">
  <div class="form-file-upload-inner">
    <label for="FileThree"><span>Select Image</span>
    </label><span class="wpcf7-form-control-wrap FileOne"><input type="file" name="FileThree" size="40" class="wpcf7-form-control wpcf7-file" id="FileThree" aria-invalid="false" /></span>
    <button class="close" type="button">x</button>
  </div>
</div>

关于javascript - 单击清除文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41206027/

相关文章:

javascript - 从指令到 Controller 共享数据

javascript - Nodejs帖子不断加载

javascript - 根据类型更改 jstree 节点文本颜色

javascript - JQuery 为什么我的 array.every 不是一个函数?

javascript - 使用 Jquery 或 Javascript 触发 mousemove 事件

java - 如何计算与 Web 应用程序代码相关的 selenium 测试的代码覆盖率

jquery - 如何动画 "visibility: hidden"?

javascript - 将文本 float 到其容器的末尾

javascript - jQuery 无法正确链接到 HTML 文件

Javascript::audio.pause 是否将 audio.currentTime 值重置为 0?