Appending a DOM element twice (jQuery)

后端 未结 5 1877
走了就别回头了
走了就别回头了 2021-02-19 22:39

Can someone explain why the following snippet does not add to both #a and #b?

HTML:

5条回答
  •  清酒与你
    2021-02-19 23:08

    You can use the .clone() method to create a new instance to append to the DOM, since your current code just refers to the same instance twice.

    $(function(){
        var $foo = $("HI");
        var $foo2 = foo.clone();
        $("#a").append($foo);
        $("#b").append($foo2);
    });
    

提交回复
热议问题