【发布时间】:2020-03-26 13:57:49
【问题描述】:
我在一张桌子上面临这种奇怪的行为,老实说,我不知道我在这里错过了什么。
我有下表:
<table>
<tr>
<th>City</th>
<th>Date</th>
<th>Description</th>
<th>Picture</th>
<th>Sort</th>
<th>
<span class="table-add mb-3 mr-2">
<a href="#!" class="text-success">
<i class="fas fa-plus fa-2x program" aria-hidden="true"></i>
</a>
</span>
</th>
</tr>
<tr>
<td>Name</td>
<td>01-01-20</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>
<div class="container-image">
<div class="avatar-upload">
<div class="avatar-edit">
<input type='file' id="imageUpload" accept=".png, .jpg, .jpeg" />
<label for="imageUpload"></label>
</div>
<div class="avatar-preview">
<div id="imagePreview" class="imagePreview" style="background-image: url(http://i.pravatar.cc/);">
</div>
</div>
</div>
</div>
</td>
<td></td>
</tr>
我有一个图标按钮来克隆我的最后一个“tr”并在我的表格底部创建一个新的。
我还有一个 jquery 函数可以将图像上传到每一行,但由于某种原因,例如,当我更改第三行中的图像时,它总是会更新我的第一行。
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#imagePreview').css('background-image', 'url('+e.target.result +')');
$(this).hide();
$(this).fadeIn(650);
};
reader.readAsDataURL(input.files[0]);
}
}
$("#imageUpload").change(function() {
readURL(this);
});
我创建了一个 JSFiddle,其中包含我的表的功能示例,以便让您对这个问题有一个确切的了解:https://jsfiddle.net/d95yo4vg/
只需添加一个新行,然后尝试更改第二行的图像。
【问题讨论】:
-
看起来你有一个额外的
</td>。 -
谢谢你,罗伯。我只是稍微清理了这段代码,以便更容易理解,并没有注意到这个额外的
。您可以在 JSFiddle 中找到功能齐全的代码。
标签: javascript jquery html dom