简体   繁体   English

追加后如何删除文档片段中的元素

[英]how to remove elements in document fragment after append

I'm working on a javascript project in which I use a document fragment to append several span elements at once. 我正在一个JavaScript项目中,在该项目中,我使用文档片段一次附加多个span元素。 Then they are animated with css. 然后,它们使用CSS进行动画处理。

.redCircle {
    float: left;
    height:20px;
    width:20px;
    border-radius:10px;
    box-shadow: 0 0 20px red;
    background-color: red;
    animation: 2s circleFall linear;
}

@keyframes circleFall {
    0%{margin:0;}
    100%{margin:100% 10%;}
}


setInterval(thingyTime, 5000);

function thingyTime() {
    var count = 15,
        spanVar = document.createElement("span"),
        fragment = document.createDocumentFragment();

    for (var i = 0; i < count; ++i) { 

        for (var j = 0; j < 1; ++j) { 
            spanVar = document.createElement('spanVar');
            spanVar.className = "redCircle";
            fragment.appendChild(spanVar);
        }
    }

    document.body.appendChild(fragment);

};

thingyTime();

I'm trying to figure out how to remove the spans after the animation is completed. 我试图弄清楚动画完成后如何删除跨度。 I have used 我用过

setTimeout(function(){

    spanVar.remove(spanVar.selectedIndex);

},2000); // <= animation length

and it works fine when it's only for one element, but I need to remove all the elements in the fragment. 当它仅用于一个元素时,它可以很好地工作,但是我需要删除片段中的所有元素。

I aprreciate your help. 非常感谢您的帮助。

Since you're already specifying a class: 由于您已经指定了一个类:

spanVar.className = "redCircle";

you can use that to select all those elements and remove them: 您可以使用它来选择所有这些元素并将其删除:

$('.redCircle').remove();

A few bits (further to my initial comment). 一点点(更进一步,我的初步评论)。 For generating, I'd drop the extra createElement("span") at the top, since it never gets used. 为了生成,我将多余的createElement("span")放在顶部,因为它从未被使用过。 Then create span's in the loop, rather than spanVar 's (assuming the are unwanted). 然后在循环中创建span,而不是spanVar (假设不需要)。 For the create element method you just give it the element type you want it to make. 对于create element方法,您只需为其指定所需的元素类型即可。

function thingyTime() {
    var count = 15,
        spanVar,
        fragment = document.createDocumentFragment();

    for (var i = 0; i < count; ++i) { 
        for (var j = 0; j < 1; ++j) { 
            spanVar = document.createElement('span');
            spanVar.className = "redCircle";
            fragment.appendChild(spanVar);
        }
    }
    document.body.appendChild(fragment);
};

For cleanup, easiest way is just use the class you created. 对于清理,最简单的方法就是使用您创建的类。 Query for em all, & then nuke em one by one. 查询全部,然后逐个核对它们。

document.querySelectorAll(".redCircle").forEach(function(c){
    c.parentNode.removeChild(c);
});

If you have other instances of the redCircle you don't want to nuke on the other hand. 另一方面,如果您还有其他的redCircle实例,则您不希望进行核举。 I'd add a var circlesToCleanup = []; 我要添加一个var circlesToCleanup = []; then in the creation loop push each of em on to it circlesToCleanup.push(spanVar) 然后在创建循环中将每个em推到其上circlesToCleanup.push(spanVar)

Then when you call your cleanup, iterate circlesToCleanup rather than the querySelector output. 然后,当您调用清理时,迭代circlesToCleanup而不是querySelector输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM