Created
July 8, 2018 14:06
-
-
Save yu-smc/0194d395e307e54710fdd14ee2991e9d to your computer and use it in GitHub Desktop.
プレビュー機能の説明
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//略 | |
$(function (){ | |
//type属性がfileのinputタグに変化があった(=ファイルが選択された)ら発火 | |
$('input[type="file"]').on('change',function previewFile(e) { | |
//変更があった要素の1個前のimg要素をpreviewとする | |
var preview = $(this).prev('img')[0] | |
//のちのち消します | |
console.log($(this).prev('img')[0]); | |
//この2行は定型の書き方で、Filereaderを使って選択されたファイルを取得 | |
var file = e.target.files[0]; | |
var reader = new FileReader(); | |
reader.onloadend = function () { | |
//取得されたファイルの結果を、previewのsrc属性に指定 | |
preview.src = reader.result; | |
} | |
if (file) { | |
reader.readAsDataURL(file); | |
} else { | |
preview.src = ""; | |
} | |
}); | |
}); | |
//略 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment