gpt4 book ai didi

jQuery追加()定位

转载 作者:行者123 更新时间:2023-12-01 06:20:33 26 4
gpt4 key购买 nike

我必须从我的网站中删除()一些iem,然后将它们追加()回来,但是当我追加它们时,它们出现在不同的地方,而我希望它们完全显示在它们以前的同一个地方是。

有什么解决办法吗?

这是一个沙箱,请随意更新(注意第一次 append 后图像如何改变位置):

http://jsfiddle.net/Zt9tZ/1/

最佳答案

也许我在这里缺少一些 append 信息,但为什么要完全删除它?有什么原因不能只使用 hideshow 吗?

http://jsfiddle.net/Zt9tZ/10/

追加的问题在于您将元素作为最后一个元素添加到父元素中。如果由于某种原因您确实必须完全删除该元素,您可以在其位置放置某种占位符。也许使用 replaceWith 和一些稍后将再次替换为原始元素的不可见元素。

<小时/>

或者,您可以跟踪最近的同级并使用 insertBeforeinsertAfter

编辑:insertAfter 的示例:

var image = jQuery(".image"); 
var prev;

jQuery("#remove").click(function(){
prev = image.prev();
image.remove();
});

jQuery("#append").click(function(){
if(prev.length === 0){
// element was first
jQuery("#container").prepend(image);
}else{
image.insertAfter(prev);
}
})

http://jsfiddle.net/Zt9tZ/12/

<小时/>

EDIT2:replaceWith 示例:

var image = jQuery(".image"); 
var replacement = $("<div></div>");

jQuery("#remove").click(function(){
image.replaceWith(replacement);
});

jQuery("#append").click(function(){
replacement.replaceWith(image);
})

http://jsfiddle.net/Zt9tZ/13/

关于jQuery追加()定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8721948/

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