jQuery .get() DOM 怪异
快速概述 - 当用户单击图像元素时,我将“选中”类添加到图像元素中。 单击我的“下一步”按钮会隐藏“选择照片”div 并显示确认 div,注入所选照片,如下所示:
$('.next').click(function () {
$('.user-photos').attr('id', 'step-two');
$('.choose-photos').hide();
var selected = $('.checked').get();
$('.confirm-batch').show().html(selected);
}
});
.get() 方法运行良好;但是,如果您单击返回步骤 1 并选择新照片,则您选择的任何照片都会从 DOM 中消失。
$('.choose').click(function (){
$('.user-photos').attr('id', 'step-one');
$('.confirm-batch').hide().html('');
$('.choose-photos').show();
}
});
有没有办法在不重新加载页面的情况下将那些“选中”的项目放回到“选择”div中?基本上只是重置进程。
Quick rundown - I'm adding a class of 'checked' to image elements when a user clicks on them.
Clicking my 'Next' button hides the 'choose-photos' div and shows the confirm div, injecting in the selected photos, like so:
$('.next').click(function () {
$('.user-photos').attr('id', 'step-two');
$('.choose-photos').hide();
var selected = $('.checked').get();
$('.confirm-batch').show().html(selected);
}
});
The .get() method is working quite well; however, if you click to back to step 1 and choose new photos, whichever photos you've selected are gone from the DOM.
$('.choose').click(function (){
$('.user-photos').attr('id', 'step-one');
$('.confirm-batch').hide().html('');
$('.choose-photos').show();
}
});
Is there a way to get those 'checked' items back in the 'choose' div without reloading the page? Basically just resetting the process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery 以愚蠢的行为重载了它的一些方法。
一个例子是它允许这样做...
看起来您正在插入从
selected
元素呈现的 HTML 字符串,但事实并非如此,它正在重新定位它们,就像您通常使用 DOM 节点一样。作为解决方案,您可以克隆它们,或者更好的是,只需将它们放回找到它们的位置,而不是使用
.html('')
销毁它们jQuery overloads some of its methods with stupid behavior.
One example is that it allows this...
It looks like you're inserting an HTML string rendered from the
selected
elements, but you're not, it's reloacting them, as you normally would with DOM nodes.As a solution, you could either clone them, or better, just put them back where you found them instead of destroying them with
.html('')