gpt4 book ai didi

javascript - 将 jQuery 中的类选择器更改为 id select

转载 作者:行者123 更新时间:2023-11-28 06:26:02 26 4
gpt4 key购买 nike

我有一些input type="file",用户可以使用它们发布图像。将会有多个输入,因此我必须将这个脚本(之前由使用类选择器创建的单个输入形成)更改为多个输入。我就想把id贴上来但它还不起作用。为什么?

function readURL(input) {
if (input.files && input.files[0]) {

var reader = new FileReader();

reader.onload = function(e) {
$('.image-upload-wrap').hide();

$('.file-upload-image').attr('src', e.target.result);
$('.file-upload-content').show();

$('.image-title').html(input.files[0].name);
};

reader.readAsDataURL(input.files[0]);

} else {
removeUpload();
}
}

function removeUpload() {
$('.file-upload-input').replaceWith($('.file-upload-input').clone());
$('.file-upload-content').hide();
$('.image-upload-wrap').show();
}
$('.image-upload-wrap').bind('dragover', function () {
$('.image-upload-wrap').addClass('image-dropping');
});
$('.image-upload-wrap').bind('dragleave', function () {
$('.image-upload-wrap').removeClass('image-dropping');
});
body {
font-family: sans-serif;
background-color: #eeeeee;
}

.file-upload {
background-color: #ffffff;
width: 600px;
margin: 0 auto;
padding: 20px;
}

.file-upload-btn {
width: 100%;
margin: 0;
color: #fff;
background: #1FB264;
border: none;
padding: 10px;
border-radius: 4px;
border-bottom: 4px solid #15824B;
transition: all .2s ease;
outline: none;
text-transform: uppercase;
font-weight: 700;
}

.file-upload-btn:hover {
background: #1AA059;
color: #ffffff;
transition: all .2s ease;
cursor: pointer;
}

.file-upload-btn:active {
border: 0;
transition: all .2s ease;
}

.file-upload-content {
display: none;
text-align: center;
}

.file-upload-input {
position: absolute;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
outline: none;
opacity: 0;
cursor: pointer;
}

.image-upload-wrap {
margin-top: 20px;
border: 4px dashed #D2D2D2;
position: relative;
}

.image-dropping,
.image-upload-wrap:hover {
background-color: #1FB264;
border: 4px dashed #D2D2D2;
}

.image-title-wrap {
padding: 0 15px 15px 15px;
color: #222;
}

.drag-text {
text-align: center;
}

.drag-text h3 {
font-weight: 100;
text-transform: uppercase;
color: #15824B;
padding: 60px 0;
}

.file-upload-image {
max-height: 200px;
max-width: 200px;
margin: auto;
padding: 20px;
}

.remove-image {
width: 200px;
margin: 0;
color: #fff;
background: #cd4535;
border: none;
padding: 10px;
border-radius: 4px;
border-bottom: 4px solid #b02818;
transition: all .2s ease;
outline: none;
text-transform: uppercase;
font-weight: 700;
}

.remove-image:hover {
background: #c13b2a;
color: #ffffff;
transition: all .2s ease;
cursor: pointer;
}

.remove-image:active {
border: 0;
transition: all .2s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="file-upload">

<button class="file-upload-btn" type="button" onclick="$('#1').trigger( 'click' )">Add Image</button>
<div class="image-upload-wrap" id="1">
<input class="file-upload-input" type='file' onchange="readURL(this);" accept="image/*" />
<div class="drag-text">
<h3>Drag and drop a file or select add Image</h3>
</div>
</div>
<div class="file-upload-content">
<img class="file-upload-image" src="#" alt="your image" />
<div class="image-title-wrap">
<button type="button" onclick="removeUpload()" class="remove-image">Remove <span class="image-title">Uploaded Image</span></button>
</div>
</div>


<button class="file-upload-btn" type="button" onclick="$('#2').trigger( 'click' )">Add Image</button>
<div class="image-upload-wrap" id="2">
<input class="file-upload-input" type='file' onchange="readURL(this);" accept="image/*" />
<div class="drag-text">
<h3>Drag and drop a file or select add Image</h3>
</div>
</div>
<div class="file-upload-content">
<img class="file-upload-image" src="#" alt="your image" />
<div class="image-title-wrap">
<button type="button" onclick="removeUpload()" class="remove-image">Remove <span class="image-title">Uploaded Image</span></button>
</div>
</div>

<button class="file-upload-btn" type="button" onclick="$('#3').trigger( 'click' )">Add Image</button>
<div class="image-upload-wrap" id="3">
<input class="file-upload-input" type='file' onchange="readURL(this);" accept="image/*" />
<div class="drag-text">
<h3>Drag and drop a file or select add Image</h3>
</div>
</div>
<div class="file-upload-content">
<img class="file-upload-image" src="#" alt="your image" />
<div class="image-title-wrap">
<button type="button" onclick="removeUpload()" class="remove-image">Remove <span class="image-title">Uploaded Image</span></button>
</div>
</div>


<button class="file-upload-btn" type="button" onclick="$('#4').trigger( 'click' )">Add Image</button>
<div class="image-upload-wrap" id="4">
<input class="file-upload-input" type='file' onchange="readURL(this);" accept="image/*" />
<div class="drag-text">
<h3>Drag and drop a file or select add Image</h3>
</div>
</div>
<div class="file-upload-content">
<img class="file-upload-image" src="#" alt="your image" />
<div class="image-title-wrap">
<button type="button" onclick="removeUpload()" class="remove-image">Remove <span class="image-title">Uploaded Image</span></button>
</div>
</div>

</div>

目前,如您所见,如果您上传照片,则在所有其他字段中都是平等的。

最佳答案

您必须使您的代码与您所使用的图像的容器相关。目前,您更改具有相同类的所有元素的外观,因此在所有容器中看到相同的内容。

查看演示 - Fiddle

对于您的 onload 函数,请使用以下命令:

reader.onload = function(e) {
var $parent = $(input).parent(),
$nextEl = $parent.next();

$parent.hide();
$nextEl.find('.file-upload-image').attr('src', e.target.result);
$nextEl.show();
$nextEl.find('.image-title').html(input.files[0].name);
};

对于您的 removeUpload 函数,请使用以下命令:

function removeUpload(button) {
var $parent = $(button).parent().parent();

$parent.hide();
$parent.prev().show();
}

并将删除按钮的 onclick 函数调用更改为 onclick="removeUpload(this)" 以将单击的按钮传递给函数。

关于javascript - 将 jQuery 中的类选择器更改为 id select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35130340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com