我正试图找出如何隐藏在目标元素之前插入的一些输出。我这样做是为了以后可以给它做动画
这就是我到目前为止所做的-但是当它插入到目标之前时,它无法隐藏输出!
$.ajax({
type: "POST",
url: ajaxURLvalue,
data: dataString,
cache: false,
success: function(html){
if(SetIn=='before') {
$('.'+followFrom+followFrom2).hide().before(html);
} else if(SetIn=='prepend') {
$('.'+followFrom+followFrom2).hide().prepend(html);
} else if(SetIn=='append') {
$('.'+followFrom+followFrom2).hide().append(html);
} else if(SetIn=='after') {
$('.'+followFrom+followFrom2).hide().after(html);
} else if(SetIn=='prependto') {
$('.'+followFrom+followFrom2).hide().prependTo(html);
}
}
});
现在,您正在隐藏目标元素,然后在其前面插入一些内容。假设您想要做的是在目标之前插入一些内容,然后隐藏插入的内容。
最直接的方法就是从你要插入的内容开始:
$(html)
…然后在目标前插入:
$(html).insertBefore('.'+followFrom+followFrom2)
…然后隐藏它:
$(html).insertBefore('.'+followFrom+followFrom2).hide();
为了保持一致,您可能希望对所有内容使用相同的格式:
if(SetIn=='before') {
$(html).insertBefore('.'+followFrom+followFrom2).hide();
} else if(SetIn=='prepend') {
$(html).prependTo('.'+followFrom+followFrom2).hide();
} else if(SetIn=='append') {
$(html).appendTo('.'+followFrom+followFrom2).hide();
} else if(SetIn=='after') {
$(html).insertAfter('.'+followFrom+followFrom2).hide();
} else if(SetIn=='prependto') {
// this one doesn't make any sense - prepending the target
// to the new elements will remove it from the document entirely!
}