How can I get a reference to a node directly after it is appended?

后端 未结 2 1680
陌清茗
陌清茗 2020-12-21 14:40

I have a situation where I\'m appending a node inside an element and would like a reference to it right away.

At the moment I do something along these lines:

<
相关标签:
2条回答
  • 2020-12-21 15:39

    You can do

    var current = $('<p>hello</p>').appendTo('#elem');
    
    0 讨论(0)
  • 2020-12-21 15:46

    Is this what you mean?

    var current = $("#elem").append("<p>hello</p>").children(':last');
    
    0 讨论(0)
提交回复
热议问题