将javascript change事件转换为jquery

时间:2013-12-17 19:29:55

标签: javascript jquery

有一个函数可以在html 5中处理文件,但是它在javascript中 我必须将其更改为jquery函数

<style>
  .thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
  }
</style>

<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,
                            '" title="', escape(theFile.name), '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>

这将在本地创建输入图像的翻滚 所以我需要改变它在jquery中工作 像这样:

$('#thisfile').change(function(){
handleFileSelect(this)
});

但是当我运行jquery函数时,它会显示TypeError: evt.target is undefined错误 我怎样才能在这里提供jquery函数参数?

1 个答案:

答案 0 :(得分:3)

$('#thisfile').change(function(evt){
    handleFileSelect(evt);
});