みんなにメリークリスマス。
4つのセクションがあり、それぞれに空のimgタグが含まれています。画像をアップロードするときは、対応するセクションに収まる必要があります。
「セクション1に画像を追加」をクリックして画像をアップロードすると、4つすべてのようにImage_1divで修正する必要があります。しかし、コードにクリック関数を挿入すると、機能しません。
ここで私の間違いは何ですか。
<div class="pre_img" >
<span>
<img class="prw_img" src="http://www.iconshock.com/img_jpg/REALVISTA/general/jpg/128/preview_icon.jpg" alt="your image" />
</span>
</div>
<input id="file" type="file" name="files[]" onChange="readURL(this);" />
<div id="Image_1">
<button> AddImage to section 1</button>
<img id="img_1" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
<div id="Image_2">
<button> AddImage to section 2</button>
<img id="img_2" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
<div id="Image_3">
<button> AddImage to section 3</button>
<img id="img_3" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
<div id="Image_4">
<button> AddImage to section 4</button>
<img id="img_4" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
これが私のスクリプトです
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.prw_img,#img_1').attr('src', e.target.result).width(112).height(112);
$('#img_1').css('display','inline');
};
reader.readAsDataURL(input.files[0]);
}
}
理解しやすいようにJSBINを作成しました。クリックを追加せずにその関数を追加しようとしましたが、スクリプト全体が機能していません。