简体   繁体   English

如何在添加节点后直接获取对节点的引用?

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

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: 目前我沿着这些方向做了些什么:

var children = $("#elem").append("<p>hello</p>").children();
var current = children[children.length -1]

Can this be done more cleanly with jQuery? 使用jQuery可以更干净地完成吗?

I know, I can write a helper function, but was wondering if there is something built in. 我知道,我可以写一个辅助函数,但想知道是否有内置的东西。

你可以做

var current = $('<p>hello</p>').appendTo('#elem');

你是这个意思吗?

var current = $("#elem").append("<p>hello</p>").children(':last');

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

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