jQuery onload代码如何可重复用于缩略图2和3

杰里米·马克(Jeremy Mark)

我正在构建的应用程序具有三个input [type =“ file”]。第一个上载输入以缩略图1为目标,第二个上载输入以第二缩略图为目标,依此类推。jQuery onload代码适用于第一个缩略图。是否有任何方法可以使代码可重复用于缩略图2和3,而无需重复代码?

$(window).on('load', function() {
  var files = $("input[type=file]");
  files.change(function(e) {
    if (this.files && this.files[0]) {
      var reader = new FileReader();
      reader.onload = function(e) {
        $(".thumbnail-one img").attr("src", e.target.result);
        $('.full-image img').attr("src", e.target.result);
      }
      reader.readAsDataURL(this.files[0]);
    }
  });
});

$(document).ready(function() {
  $('.box').click(function() {
    $('.full-image').html($(this).html());
    console.log(this);
  });
});
body {
  font-family: 'Poppins', Verdana, Arial, sans-serif;
}

fieldset {
  background-color: lavender;
  border: 10px solid darkblue;
  border-radius: 20px;
  margin: 20px auto;
  width: 720px;
}

legend {
  background-color: purple;
  border-radius: 10px;
  color: white;
  padding: 12px;
}

fieldset div {
  margin: 10px;
}

label {
  display: inline-block;
  text-align: right;
  vertical-align: top;
  width: 200px;
}

.container {
  width: 60%;
  overflow: hidden;
  margin: 100px auto;
}

.box {
  width: 100px;
  height: auto;
  padding: 10px;
}

.box {
  cursor: pointer;
}

.full-image {
  width: 580px;
  height: 580px;
  padding: 10px;
}

.col {
  float: right;
}

.full-image img {
  width: 100%;
  /* height: 100%; */
}

.closebtn {
  position: absolute;
  top: 10px;
  right: 15px;
  color: white;
  font-size: 35px;
  cursor: pointer;
}
<title>Image Gallery App</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<fieldset>
  <legend>Your Images</legend>
  <div>
    <label for="avatar">Upload Your Image:</label>
    <input type="file" id="avatar" name="avatar" required="">
  </div>
  <div>
    <label for="avatar">Upload Your Image:</label>
    <input type="file" id="avatar" name="avatar" required="">
  </div>
  <div>
    <label for="avatar">Upload Your Image:</label>
    <input type="file" id="avatar" name="avatar" required="">
  </div>
</fieldset>

<div class="container">
  <div class="col">
    <div class="box thumbnail-one">
      <img src="https://http.cat/100" alt="Nature" style="width:100%">
    </div>
    <div class="box thumbnail-two">
      <img src="https://http.cat/405" alt="Snow" style="width:100%">
    </div>
    <div class="box thumbnail-three">
      <img src="https://http.cat/504" alt="Mountains" style="width:100%">
    </div>
  </div>
  <div class="col">
    <div class="full-image">
      <span onclick="this.parentElement.style.display='none'" class="closebtn">&times;</span>
      <img src="https://http.cat/100" id="expandedImg" />
    </div>
  </div>
</div>

卡兰
  1. attribute data-thumb向您的输入添加新内容,并在此处添加相应的缩略图类data-thumb='thumbnail-one' data-thumb='thumbnail-two'您的输入将如下所示<input type="file" id="avatar" name="avatar" data-thumb='one' required="">

  2. 用于let thumb = $(this).data('thumb');在中获取缩略图类的值files.change(function(e) {...}请注意,您也可以使用let thumb = $(this).attr('data-thumb');

  3. 使用$("." + thumb + " img").attr("src", e.target.result);的选择。

PS正如@AlwaysHelping在评论中提到的那样,并且按照建议,您应始终使用唯一id值。因为如果使用#avatar它,它将始终firstDOMwith中返回element id = avatar同样在形式上postback也可能引起问题。

$(window).on('load', function() {
  var files = $("input[type=file]");
  files.change(function(e) {
    if (this.files && this.files[0]) {
      let thumb = $(this).data('thumb');
      // let thumb = $(this).attr('data-thumb'); // alternative to above line.
      var reader = new FileReader();
      reader.onload = function(e) {
        $("." + thumb + " img").attr("src", e.target.result);
        $('.full-image img').attr("src", e.target.result);
      }
      reader.readAsDataURL(this.files[0]);
    }
  });
});

$(document).ready(function() {
  $('.box').click(function() {
    $('.full-image').html($(this).html());
    console.log(this);
  });
});
body {
  font-family: 'Poppins', Verdana, Arial, sans-serif;
}

fieldset {
  background-color: lavender;
  border: 10px solid darkblue;
  border-radius: 20px;
  margin: 20px auto;
  width: 720px;
}

legend {
  background-color: purple;
  border-radius: 10px;
  color: white;
  padding: 12px;
}

fieldset div {
  margin: 10px;
}

label {
  display: inline-block;
  text-align: right;
  vertical-align: top;
  width: 200px;
}

.container {
  width: 60%;
  overflow: hidden;
  margin: 100px auto;
}

.box {
  width: 100px;
  height: auto;
  padding: 10px;
}

.box {
  cursor: pointer;
}

.full-image {
  width: 580px;
  height: 580px;
  padding: 10px;
}

.col {
  float: right;
}

.full-image img {
  width: 100%;
  /* height: 100%; */
}

.closebtn {
  position: absolute;
  top: 10px;
  right: 15px;
  color: white;
  font-size: 35px;
  cursor: pointer;
}
<title>Image Gallery App</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<fieldset>
  <legend>Your Images</legend>
  <div>
    <label for="avatar">Upload Your Image:</label>
    <input type="file" id="avatar1" name="avatar1" data-thumb='thumbnail-one' required="">
  </div>
  <div>
    <label for="avatar">Upload Your Image:</label>
    <input type="file" id="avatar2" name="avatar2" data-thumb='thumbnail-two' required="">
  </div>
  <div>
    <label for="avatar">Upload Your Image:</label>
    <input type="file" id="avatar3" name="avatar3" data-thumb='thumbnail-three' required="">
  </div>
</fieldset>

<div class="container">
  <div class="col">
    <div class="box thumbnail-one">
      <img src="https://http.cat/100" alt="Nature" style="width:100%">
    </div>
    <div class="box thumbnail-two">
      <img src="https://http.cat/405" alt="Snow" style="width:100%">
    </div>
    <div class="box thumbnail-three">
      <img src="https://http.cat/504" alt="Mountains" style="width:100%">
    </div>
  </div>
  <div class="col">
    <div class="full-image">
      <span onclick="this.parentElement.style.display='none'" class="closebtn">&times;</span>
      <img src="https://http.cat/100" id="expandedImg" />
    </div>
  </div>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用HTML5和Jquery从视频中捕获缩略图(帧)?

jQuery Mobile缩略图对齐

jQuery缩略图悬停弹出窗口

使用jQuery删除引导缩略图

使用Javascript(而不是jQuery)单击缩略图后如何显示图片?

如何在jQuery mobile 1.4.0中将列表视图缩略图放置在右侧?

使用jQuery Chosen插件后如何添加缩略图以下拉?

如何通过使用视频URL为jquery jplayer创建缩略图

jQuery 如何在单击图像缩略图时显示图像加载器

在jQuery移动列表视图中水平对齐缩略图

jQuery基本滑块带缩略图的图像库

BlueImp jQuery文件上传-返回缩略图URL

在jQuery中延迟加载列表缩略图

jQuery在上传之前显示多个图像缩略图

如何从jquery中的图像列表中将第一个选定图像作为缩略图图像?

模板中的if语句可正确分配缩略图和实际尺寸的图像以用于Fancybox

缩小jQuery Mobile ListView缩略图和文本之间的距离

Simplepie rss缺少缩略图,使用jQuery的替代解决方案

jQuery Dropzone.js将缩略图宽度更改为100%

jQuery:为每个单选按钮分配一个缩略图SRC,然后使用它

jQuery的幻灯片幻灯片没有缩略图?

jquery-ui在下拉列表中显示缩略图

使用ASP.NET MVC 4的jQuery Dialog悬停缩略图

jQuery.fileuploadui,在ie9中不显示缩略图预览

使用下一个和上一个图像箭头进行导航的JS / jQuery数组循环缩略图

EXIF和缩略图

如何通过C#代码在Android的Unity3D中创建图像缩略图?

如何显示缩略图?

Bootstrap 3-网格系统和缩略图