admin管理员组文章数量:1024640
I have file input where after selecting image file a image preview is shown, Here is the jquery code
<script>
var loadFile = function(event) {
var output = document.getElementById('output');
output.src = URL.createObjectURL(event.target.files[0]);
};
</script>
Html code->
<input type="file" name="image_file" id="cretor" accept="image/*" onchange="loadFile(event)">
<img id="output" width="80" height="80">
The above codes generate image preview instantly after selecting an image but how can i show delete button in the top right corner of image and reset the file input and remove that image
here is what I've tried to show delete button at top right corner of image
<div class="img-wrap">
<button id="clear" onclick="twz()">x</a>
<img src="blob:" id="output">
</div>
CSS
.img-wrap {
position: relative;
}
.img-wrap .close {
position: absolute;
top: 2px;
right: 2px;
z-index: 100;
}
Now how can i generate button and place it in the right corner because loadFile function only generate image preview
I have file input where after selecting image file a image preview is shown, Here is the jquery code
<script>
var loadFile = function(event) {
var output = document.getElementById('output');
output.src = URL.createObjectURL(event.target.files[0]);
};
</script>
Html code->
<input type="file" name="image_file" id="cretor" accept="image/*" onchange="loadFile(event)">
<img id="output" width="80" height="80">
The above codes generate image preview instantly after selecting an image but how can i show delete button in the top right corner of image and reset the file input and remove that image
here is what I've tried to show delete button at top right corner of image
<div class="img-wrap">
<button id="clear" onclick="twz()">x</a>
<img src="blob:https://www.example./c880bfa4-25d0-4e2b-91e4-d722b864cc79" id="output">
</div>
CSS
.img-wrap {
position: relative;
}
.img-wrap .close {
position: absolute;
top: 2px;
right: 2px;
z-index: 100;
}
Now how can i generate button and place it in the right corner because loadFile function only generate image preview
Share Improve this question asked Apr 24, 2016 at 20:02 Aniket SinghAniket Singh 8774 gold badges17 silver badges42 bronze badges 1- Quick observation... <button id="clear" onclick="twz()">x</a> has an anchor closing tag. – user6245342 Commented Apr 24, 2016 at 20:15
1 Answer
Reset to default 3There is 2 mistakes in your code:
1- you didn't close correctly your button
so the right markup should be:
<input type="file" name="image_file" id="cretor" accept="image/*"onchange="loadFile(event)">
<div class="img-wrap">
<button id="clear" class="hide" onclick="twz()">x</button>
<img id="output" width="80" height="80">
2- The second is that in your css you try to select a class named close
, but the markup has a id="clear"
so you can change your css in this way and it will work fine:
.img-wrap {
position: relative;
float:left;
}
.img-wrap #clear {
position: absolute;
top: 2px;
right: 2px;
z-index: 100;
}
.hide{
display:none;
}
You can show the button only when the image is choosen removing the class hide
from the button. Your script bees:
<script>
var loadFile = function(event) {
var output = document.getElementById('output');
output.src = URL.createObjectURL(event.target.files[0]);
$("#clear").removeClass("hide");
};
</script>
I have file input where after selecting image file a image preview is shown, Here is the jquery code
<script>
var loadFile = function(event) {
var output = document.getElementById('output');
output.src = URL.createObjectURL(event.target.files[0]);
};
</script>
Html code->
<input type="file" name="image_file" id="cretor" accept="image/*" onchange="loadFile(event)">
<img id="output" width="80" height="80">
The above codes generate image preview instantly after selecting an image but how can i show delete button in the top right corner of image and reset the file input and remove that image
here is what I've tried to show delete button at top right corner of image
<div class="img-wrap">
<button id="clear" onclick="twz()">x</a>
<img src="blob:" id="output">
</div>
CSS
.img-wrap {
position: relative;
}
.img-wrap .close {
position: absolute;
top: 2px;
right: 2px;
z-index: 100;
}
Now how can i generate button and place it in the right corner because loadFile function only generate image preview
I have file input where after selecting image file a image preview is shown, Here is the jquery code
<script>
var loadFile = function(event) {
var output = document.getElementById('output');
output.src = URL.createObjectURL(event.target.files[0]);
};
</script>
Html code->
<input type="file" name="image_file" id="cretor" accept="image/*" onchange="loadFile(event)">
<img id="output" width="80" height="80">
The above codes generate image preview instantly after selecting an image but how can i show delete button in the top right corner of image and reset the file input and remove that image
here is what I've tried to show delete button at top right corner of image
<div class="img-wrap">
<button id="clear" onclick="twz()">x</a>
<img src="blob:https://www.example./c880bfa4-25d0-4e2b-91e4-d722b864cc79" id="output">
</div>
CSS
.img-wrap {
position: relative;
}
.img-wrap .close {
position: absolute;
top: 2px;
right: 2px;
z-index: 100;
}
Now how can i generate button and place it in the right corner because loadFile function only generate image preview
Share Improve this question asked Apr 24, 2016 at 20:02 Aniket SinghAniket Singh 8774 gold badges17 silver badges42 bronze badges 1- Quick observation... <button id="clear" onclick="twz()">x</a> has an anchor closing tag. – user6245342 Commented Apr 24, 2016 at 20:15
1 Answer
Reset to default 3There is 2 mistakes in your code:
1- you didn't close correctly your button
so the right markup should be:
<input type="file" name="image_file" id="cretor" accept="image/*"onchange="loadFile(event)">
<div class="img-wrap">
<button id="clear" class="hide" onclick="twz()">x</button>
<img id="output" width="80" height="80">
2- The second is that in your css you try to select a class named close
, but the markup has a id="clear"
so you can change your css in this way and it will work fine:
.img-wrap {
position: relative;
float:left;
}
.img-wrap #clear {
position: absolute;
top: 2px;
right: 2px;
z-index: 100;
}
.hide{
display:none;
}
You can show the button only when the image is choosen removing the class hide
from the button. Your script bees:
<script>
var loadFile = function(event) {
var output = document.getElementById('output');
output.src = URL.createObjectURL(event.target.files[0]);
$("#clear").removeClass("hide");
};
</script>
本文标签: javascripthow to show delete button in right corner of ImageStack Overflow
版权声明:本文标题:javascript - how to show delete button in right corner of Image - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745609116a2158899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论