temporarily removing and later reinserting a DOM element?

前端 未结 4 1223
囚心锁ツ
囚心锁ツ 2021-02-18 20:25

Is there some jquery magic that will let me do the following:

[0- define some element in HTML (eg, a unchecked checkbox)]

1- update its DOM element by s

4条回答
  •  终归单人心
    2021-02-18 21:02

    You may use the clone method:

    var els = $('.els'), saved = els.clone (true);
    els.remove ();
    // .... do other stuff
    saved.appendTo ($('.wherever-you-want-to'));
    

    That said, though, it's better to show & hide them (via display: none, for example), than to manipulate the DOM as it's very expensive. If you have to, use DOM insertion & removal (as above), rather than .html (), which recreated a node from the given string every time.

提交回复
热议问题